diff options
author | 2023-01-23 09:47:33 -0500 | |
---|---|---|
committer | 2023-01-23 09:47:33 -0500 | |
commit | f5adbd6b55ca13a7523dff2cfc5dccdab9980fa7 (patch) | |
tree | b7955f7140c8ea757687441a5ac307e4db4b884d /packages/integrations/netlify/test | |
parent | 9e57268f1318853b612711b31d7461e9b9ce1978 (diff) | |
download | astro-f5adbd6b55ca13a7523dff2cfc5dccdab9980fa7.tar.gz astro-f5adbd6b55ca13a7523dff2cfc5dccdab9980fa7.tar.zst astro-f5adbd6b55ca13a7523dff2cfc5dccdab9980fa7.zip |
Support prerender in Netlify redirects (#5904)
* Support prerender in Netlify redirects
* Updated sorting algorithm
* Update packages/integrations/netlify/src/shared.ts
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com>
Diffstat (limited to 'packages/integrations/netlify/test')
8 files changed, 131 insertions, 1 deletions
diff --git a/packages/integrations/netlify/test/functions/dynamic-route.test.js b/packages/integrations/netlify/test/functions/dynamic-route.test.js index 0cfb5359b..6bb68eab8 100644 --- a/packages/integrations/netlify/test/functions/dynamic-route.test.js +++ b/packages/integrations/netlify/test/functions/dynamic-route.test.js @@ -21,6 +21,13 @@ describe('Dynamic pages', () => { it('Dynamic pages are included in the redirects file', async () => { const redir = await fixture.readFile('/_redirects'); - expect(redir).to.match(/\/products\/\*/); + expect(redir).to.match(/\/products\/:id/); + }); + + it('Prerendered routes are also included using placeholder syntax', async () => { + const redir = await fixture.readFile('/_redirects'); + expect(redir).to.include('/pets/:cat /pets/:cat/index.html 200'); + expect(redir).to.include('/pets/:dog /pets/:dog/index.html 200'); + expect(redir).to.include('/pets /.netlify/functions/entry 200'); }); }); diff --git a/packages/integrations/netlify/test/functions/fixtures/dynamic-route/src/pages/pets/[cat].astro b/packages/integrations/netlify/test/functions/fixtures/dynamic-route/src/pages/pets/[cat].astro new file mode 100644 index 000000000..f86ee6ca9 --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/dynamic-route/src/pages/pets/[cat].astro @@ -0,0 +1,27 @@ +--- +export const prerender = true + +export function getStaticPaths() { + return [ + { + params: {cat: 'cat1'}, + props: {cat: 'cat1'} + }, + { + params: {cat: 'cat2'}, + props: {cat: 'cat2'} + }, + { + params: {cat: 'cat3'}, + props: {cat: 'cat3'} + }, + ]; +} + +const { cat } = Astro.props; + +--- + +<div>Good cat, {cat}!</div> + +<a href="/">back</a> diff --git a/packages/integrations/netlify/test/functions/fixtures/dynamic-route/src/pages/pets/[dog].astro b/packages/integrations/netlify/test/functions/fixtures/dynamic-route/src/pages/pets/[dog].astro new file mode 100644 index 000000000..0f3300f04 --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/dynamic-route/src/pages/pets/[dog].astro @@ -0,0 +1,27 @@ +--- +export const prerender = true + +export function getStaticPaths() { + return [ + { + params: {dog: 'dog1'}, + props: {dog: 'dog1'} + }, + { + params: {dog: 'dog2'}, + props: {dog: 'dog2'} + }, + { + params: {dog: 'dog3'}, + props: {dog: 'dog3'} + }, + ]; +} + +const { dog } = Astro.props; + +--- + +<div>Good dog, {dog}!</div> + +<a href="/">back</a> diff --git a/packages/integrations/netlify/test/functions/fixtures/dynamic-route/src/pages/pets/index.astro b/packages/integrations/netlify/test/functions/fixtures/dynamic-route/src/pages/pets/index.astro new file mode 100644 index 000000000..d1423f8ef --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/dynamic-route/src/pages/pets/index.astro @@ -0,0 +1,12 @@ +<html lang="en"> + <head> + <meta charset="utf-8" /> + <link rel="icon" type="image/svg+xml" href="/favicon.svg" /> + <meta name="viewport" content="width=device-width" /> + <meta name="generator" content={Astro.generator} /> + <title>Astro</title> + </head> + <body> + <h1>Astro</h1> + </body> +</html> diff --git a/packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/404.astro b/packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/404.astro new file mode 100644 index 000000000..ad5d44aa2 --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/404.astro @@ -0,0 +1,8 @@ +<html> +<head> + <title>Testing</title> +</head> +<body> + <h1>testing</h1> +</body> +</html> diff --git a/packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/index.astro b/packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/index.astro new file mode 100644 index 000000000..ad5d44aa2 --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/index.astro @@ -0,0 +1,8 @@ +<html> +<head> + <title>Testing</title> +</head> +<body> + <h1>testing</h1> +</body> +</html> diff --git a/packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/one.astro b/packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/one.astro new file mode 100644 index 000000000..12146450e --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/one.astro @@ -0,0 +1,11 @@ +--- +export const prerender = true; +--- +<html> +<head> + <title>Testing</title> +</head> +<body> + <h1>testing</h1> +</body> +</html> diff --git a/packages/integrations/netlify/test/functions/prerender.test.js b/packages/integrations/netlify/test/functions/prerender.test.js new file mode 100644 index 000000000..324ebc5c5 --- /dev/null +++ b/packages/integrations/netlify/test/functions/prerender.test.js @@ -0,0 +1,30 @@ +import { expect } from 'chai'; +import netlifyAdapter from '../../dist/index.js'; +import { loadFixture, testIntegration } from './test-utils.js'; + +describe('Mixed Prerendering with SSR', () => { + /** @type {import('./test-utils').Fixture} */ + let fixture; + + before(async () => { + fixture = await loadFixture({ + root: new URL('./fixtures/prerender/', import.meta.url).toString(), + output: 'server', + adapter: netlifyAdapter({ + dist: new URL('./fixtures/prerender/dist/', import.meta.url), + }), + site: `http://example.com`, + integrations: [testIntegration()], + }); + await fixture.build(); + }); + it('Wildcard 404 is sorted last', async () => { + const redir = await fixture.readFile('/_redirects'); + const baseRouteIndex = redir.indexOf('/ /.netlify/functions/entry 200'); + const oneRouteIndex = redir.indexOf('/one /one/index.html 200'); + const fourOhFourWildCardIndex = redir.indexOf('/* /.netlify/functions/entry 404'); + + expect(fourOhFourWildCardIndex).to.be.greaterThan(baseRouteIndex); + expect(fourOhFourWildCardIndex).to.be.greaterThan(oneRouteIndex); + }); +}); |