Add 404 routing logic to Netlify redirects file (#4274)

* Add 404 routing logic to Netlify redirects file

* changeset
This commit is contained in:
Matthew Phillips 2022-08-11 19:26:32 -04:00 committed by GitHub
parent be6470688f
commit d3d09a2c9f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 59 additions and 0 deletions

View file

@ -0,0 +1,5 @@
---
'@astrojs/netlify': patch
---
Adds 404 routing logic to Netlify redirects file

View file

@ -16,6 +16,11 @@ export async function createRedirects(
if (route.pathname) {
_redirects += `
${route.pathname} /.netlify/${kind}/${entryFile} 200`;
if(route.route === '/404') {
_redirects += `
/* /.netlify/${kind}/${entryFile} 404`;
}
} else {
const pattern =
'/' + route.segments.map(([part]) => (part.dynamic ? '*' : part.content)).join('/');

View file

@ -0,0 +1,27 @@
import { expect } from 'chai';
import netlifyAdapter from '../../dist/index.js';
import { loadFixture, testIntegration } from './test-utils.js';
describe('404 page', () => {
/** @type {import('./test-utils').Fixture} */
let fixture;
before(async () => {
fixture = await loadFixture({
root: new URL('./fixtures/404/', import.meta.url).toString(),
output: 'server',
adapter: netlifyAdapter({
dist: new URL('./fixtures/404/dist/', import.meta.url),
}),
site: `http://example.com`,
integrations: [testIntegration()],
});
await fixture.build();
});
it('404 route is included in the redirect file', async () => {
const redir = await fixture.readFile('/_redirects');
const expr = new RegExp("/* /.netlify/functions/entry 404");
expect(redir).to.match(expr);
});
});

View file

@ -0,0 +1,11 @@
---
---
<html>
<head>
<title>Not found</title>
</head>
<body>
<h1>Not found</h1>
</body>
</html>

View file

@ -0,0 +1,11 @@
---
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
</body>
</html>