Skip to content

Commit

Permalink
Add 404 routing logic to Netlify redirects file (#4274)
Browse files Browse the repository at this point in the history
* Add 404 routing logic to Netlify redirects file

* changeset
  • Loading branch information
matthewp committed Aug 11, 2022
1 parent be64706 commit d3d09a2
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/orange-pens-live.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/netlify': patch
---

Adds 404 routing logic to Netlify redirects file
5 changes: 5 additions & 0 deletions packages/integrations/netlify/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('/');
Expand Down
27 changes: 27 additions & 0 deletions packages/integrations/netlify/test/functions/404.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
---
<html>
<head>
<title>Not found</title>
</head>
<body>
<h1>Not found</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>Testing</h1>
</body>
</html>

0 comments on commit d3d09a2

Please sign in to comment.