diff --git a/.changeset/orange-pens-live.md b/.changeset/orange-pens-live.md new file mode 100644 index 000000000..a88792971 --- /dev/null +++ b/.changeset/orange-pens-live.md @@ -0,0 +1,5 @@ +--- +'@astrojs/netlify': patch +--- + +Adds 404 routing logic to Netlify redirects file diff --git a/packages/integrations/netlify/src/shared.ts b/packages/integrations/netlify/src/shared.ts index 90bd5333b..2ae52986d 100644 --- a/packages/integrations/netlify/src/shared.ts +++ b/packages/integrations/netlify/src/shared.ts @@ -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('/'); diff --git a/packages/integrations/netlify/test/functions/404.test.js b/packages/integrations/netlify/test/functions/404.test.js new file mode 100644 index 000000000..1bb5d90bd --- /dev/null +++ b/packages/integrations/netlify/test/functions/404.test.js @@ -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); + }); +}); diff --git a/packages/integrations/netlify/test/functions/fixtures/404/src/pages/404.astro b/packages/integrations/netlify/test/functions/fixtures/404/src/pages/404.astro new file mode 100644 index 000000000..b60b5e55a --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/404/src/pages/404.astro @@ -0,0 +1,11 @@ +--- + +--- + +
+