Skip to content

Commit 35fa965

Browse files
authored
Add 404 routing logic to Netlify redirects file (#4274)
* Add 404 routing logic to Netlify redirects file * changeset
1 parent 00338b8 commit 35fa965

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

packages/integrations/netlify/src/shared.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ export async function createRedirects(
1616
if (route.pathname) {
1717
_redirects += `
1818
${route.pathname} /.netlify/${kind}/${entryFile} 200`;
19+
20+
if(route.route === '/404') {
21+
_redirects += `
22+
/* /.netlify/${kind}/${entryFile} 404`;
23+
}
1924
} else {
2025
const pattern =
2126
'/' + route.segments.map(([part]) => (part.dynamic ? '*' : part.content)).join('/');
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { expect } from 'chai';
2+
import netlifyAdapter from '../../dist/index.js';
3+
import { loadFixture, testIntegration } from './test-utils.js';
4+
5+
describe('404 page', () => {
6+
/** @type {import('./test-utils').Fixture} */
7+
let fixture;
8+
9+
before(async () => {
10+
fixture = await loadFixture({
11+
root: new URL('./fixtures/404/', import.meta.url).toString(),
12+
output: 'server',
13+
adapter: netlifyAdapter({
14+
dist: new URL('./fixtures/404/dist/', import.meta.url),
15+
}),
16+
site: `http://example.com`,
17+
integrations: [testIntegration()],
18+
});
19+
await fixture.build();
20+
});
21+
22+
it('404 route is included in the redirect file', async () => {
23+
const redir = await fixture.readFile('/_redirects');
24+
const expr = new RegExp("/* /.netlify/functions/entry 404");
25+
expect(redir).to.match(expr);
26+
});
27+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
3+
---
4+
<html>
5+
<head>
6+
<title>Not found</title>
7+
</head>
8+
<body>
9+
<h1>Not found</h1>
10+
</body>
11+
</html>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
3+
---
4+
<html>
5+
<head>
6+
<title>Testing</title>
7+
</head>
8+
<body>
9+
<h1>Testing</h1>
10+
</body>
11+
</html>

0 commit comments

Comments
 (0)