From 00224f314344a891f94a0e359bd5b86b7b1f3077 Mon Sep 17 00:00:00 2001 From: Matthew Phillips Date: Thu, 19 Jan 2023 14:13:21 -0500 Subject: [PATCH] Updated sorting algorithm --- packages/integrations/netlify/src/shared.ts | 33 ++++++++++++------- .../fixtures/prerender/src/pages/404.astro | 8 +++++ .../fixtures/prerender/src/pages/index.astro | 8 +++++ .../fixtures/prerender/src/pages/one.astro | 11 +++++++ .../netlify/test/functions/prerender.test.js | 30 +++++++++++++++++ 5 files changed, 78 insertions(+), 12 deletions(-) create mode 100644 packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/404.astro create mode 100644 packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/index.astro create mode 100644 packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/one.astro create mode 100644 packages/integrations/netlify/test/functions/prerender.test.js diff --git a/packages/integrations/netlify/src/shared.ts b/packages/integrations/netlify/src/shared.ts index c6ae5fb24e7c..26fa49413272 100644 --- a/packages/integrations/netlify/src/shared.ts +++ b/packages/integrations/netlify/src/shared.ts @@ -5,6 +5,7 @@ type RedirectDefinition = { dynamic: boolean; input: string; target: string; + weight: 0 | 1; status: 200 | 404; }; @@ -26,15 +27,17 @@ export async function createRedirects( definitions.push({ dynamic: false, input: route.pathname, - target: route.distURL.toString().replace(dir.toString(), ''), - status: 200 + target: prependForwardSlash(route.distURL.toString().replace(dir.toString(), '')), + status: 200, + weight: 1 }); } else { definitions.push({ dynamic: false, input: route.pathname, target: `/.netlify/${kind}/${entryFile}`, - status: 200 + status: 200, + weight: 1, }); if (route.route === '/404') { @@ -42,7 +45,8 @@ export async function createRedirects( dynamic: true, input: '/*', target: `/.netlify/${kind}/${entryFile}`, - status: 404 + status: 404, + weight: 0 }); } } @@ -67,14 +71,16 @@ export async function createRedirects( dynamic: true, input: pattern, target, - status: 200 + status: 200, + weight: 1 }); } else { definitions.push({ dynamic: true, input: pattern, target: `/.netlify/${kind}/${entryFile}`, - status: 200 + status: 200, + weight: 1 }); } } @@ -94,22 +100,21 @@ function prettify(definitions: RedirectDefinition[]) { // Find the longest input, so we can format things nicely if(a.input.length > minInputLength) { minInputLength = a.input.length; - } else if(b.input.length > minInputLength) { + } + if(b.input.length > minInputLength) { minInputLength = b.input.length; } // Same for the target if(a.target.length > minTargetLength) { minTargetLength = a.target.length; - } else if(b.target.length > minTargetLength) { + } + if(b.target.length > minTargetLength) { minTargetLength = b.target.length; } // Sort dynamic routes on top - if(a.dynamic === b.dynamic) { - // If both are the same, sort alphabetically - return a.input > b.input ? 1 : -1; - } else if(a.dynamic) { + if(a.weight > b.weight) { return -1; } else { return 1; @@ -127,3 +132,7 @@ function prettify(definitions: RedirectDefinition[]) { }); return _redirects; } + +function prependForwardSlash(str: string) { + return str[0] === '/' ? str : '/' + str; +} 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 000000000000..ad5d44aa213b --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/404.astro @@ -0,0 +1,8 @@ + + + Testing + + +

testing

+ + 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 000000000000..ad5d44aa213b --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/index.astro @@ -0,0 +1,8 @@ + + + Testing + + +

testing

+ + 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 000000000000..12146450e41c --- /dev/null +++ b/packages/integrations/netlify/test/functions/fixtures/prerender/src/pages/one.astro @@ -0,0 +1,11 @@ +--- +export const prerender = true; +--- + + + Testing + + +

testing

+ + 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 000000000000..324ebc5c5a10 --- /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); + }); +});