Skip to content

Commit

Permalink
Updated sorting algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewp committed Jan 19, 2023
1 parent a763659 commit 00224f3
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 12 deletions.
33 changes: 21 additions & 12 deletions packages/integrations/netlify/src/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ type RedirectDefinition = {
dynamic: boolean;
input: string;
target: string;
weight: 0 | 1;
status: 200 | 404;
};

Expand All @@ -26,23 +27,26 @@ 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') {
definitions.push({
dynamic: true,
input: '/*',
target: `/.netlify/${kind}/${entryFile}`,
status: 404
status: 404,
weight: 0
});
}
}
Expand All @@ -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
});
}
}
Expand All @@ -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;
Expand All @@ -127,3 +132,7 @@ function prettify(definitions: RedirectDefinition[]) {
});
return _redirects;
}

function prependForwardSlash(str: string) {
return str[0] === '/' ? str : '/' + str;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>testing</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>testing</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
export const prerender = true;
---
<html>
<head>
<title>Testing</title>
</head>
<body>
<h1>testing</h1>
</body>
</html>
30 changes: 30 additions & 0 deletions packages/integrations/netlify/test/functions/prerender.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 00224f3

Please sign in to comment.