Add 404 routing logic to Netlify redirects file (#4274)
* Add 404 routing logic to Netlify redirects file * changeset
This commit is contained in:
parent
be6470688f
commit
d3d09a2c9f
5 changed files with 59 additions and 0 deletions
5
.changeset/orange-pens-live.md
Normal file
5
.changeset/orange-pens-live.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'@astrojs/netlify': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Adds 404 routing logic to Netlify redirects file
|
|
@ -16,6 +16,11 @@ export async function createRedirects(
|
||||||
if (route.pathname) {
|
if (route.pathname) {
|
||||||
_redirects += `
|
_redirects += `
|
||||||
${route.pathname} /.netlify/${kind}/${entryFile} 200`;
|
${route.pathname} /.netlify/${kind}/${entryFile} 200`;
|
||||||
|
|
||||||
|
if(route.route === '/404') {
|
||||||
|
_redirects += `
|
||||||
|
/* /.netlify/${kind}/${entryFile} 404`;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
const pattern =
|
const pattern =
|
||||||
'/' + route.segments.map(([part]) => (part.dynamic ? '*' : part.content)).join('/');
|
'/' + route.segments.map(([part]) => (part.dynamic ? '*' : part.content)).join('/');
|
||||||
|
|
27
packages/integrations/netlify/test/functions/404.test.js
Normal file
27
packages/integrations/netlify/test/functions/404.test.js
Normal 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);
|
||||||
|
});
|
||||||
|
});
|
|
@ -0,0 +1,11 @@
|
||||||
|
---
|
||||||
|
|
||||||
|
---
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Not found</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Not found</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
|
@ -0,0 +1,11 @@
|
||||||
|
---
|
||||||
|
|
||||||
|
---
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>Testing</title>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>Testing</h1>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Add table
Reference in a new issue