Skip to content
This repository was archived by the owner on May 10, 2021. It is now read-only.

Commit ebc0905

Browse files
committed
Do not create rewrites for SSG & HTML pages with static routing
Pages that are statically generated (using getStaticProps)) or pre-rendered (plain HTML) do not require an entry in the redirects file thanks to shadowing: https://docs.netlify.com/routing/redirects/rewrites-proxies/#shadowing Having too many rewrites or redirects in the _redirects file can negatively impact performance: https://community.netlify.com/t/max-number-of-redirects/5095/2 See: #26
1 parent cef50d0 commit ebc0905

File tree

6 files changed

+40
-40
lines changed

6 files changed

+40
-40
lines changed

lib/setupRedirects.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,27 @@ const getNetlifyFunctionName = require('./getNetlifyFunctionName')
1515
const setupRedirects = () => {
1616
console.log("\x1b[1m🔀 Setting up redirects\x1b[22m")
1717

18+
// Step 1: Collect custom redirects defined by the user
19+
const customRedirects = []
20+
if(existsSync(CUSTOM_REDIRECTS_PATH)) {
21+
console.log(" ", "# Prepending custom redirects")
22+
customRedirects.push(readFileSync(CUSTOM_REDIRECTS_PATH))
23+
}
24+
25+
// Step 2: Generate redirects for NextJS pages
26+
27+
// Identify pages that require redirects:
28+
// If the page is statically routed and the page is HTML or SSG, we do not
29+
// need to add an entry to the redirects file. Netlify automatically routes to
30+
// these static pages.
31+
// See: https://github.com/netlify/next-on-netlify/issues/26
32+
const pagesNeedingRedirect = allNextJsPages.filter(page =>
33+
isDynamicRoute(page.route) || page.isSsr() || page.isSsgFallback()
34+
)
35+
1836
// Identify static and dynamically routed pages
19-
const staticPages = allNextJsPages.filter(({ route }) => !isDynamicRoute(route))
20-
const dynamicPages = allNextJsPages.filter(({ route }) => isDynamicRoute(route))
37+
const staticPages = pagesNeedingRedirect.filter(({ route }) => !isDynamicRoute(route))
38+
const dynamicPages = pagesNeedingRedirect.filter(({ route }) => isDynamicRoute(route))
2139

2240
// Sort dynamic pages by route: More-specific routes precede less-specific
2341
// routes
@@ -30,14 +48,8 @@ const setupRedirects = () => {
3048
// Assemble sorted pages: static pages first, then sorted dynamic pages
3149
const sortedPages = [...staticPages, ...sortedDynamicPages]
3250

33-
// Generate redirects as array
34-
const redirects = []
35-
if(existsSync(CUSTOM_REDIRECTS_PATH)) {
36-
console.log(" ", "# Prepending custom redirects")
37-
redirects.push(readFileSync(CUSTOM_REDIRECTS_PATH))
38-
}
39-
redirects.push("# Next-on-Netlify Redirects")
40-
51+
// Generate redirects for nextJS pages
52+
const nextOnNetlifyRedirects = []
4153
sortedPages.forEach(page => {
4254
// Generate redirect for each page route
4355
page.routesAsArray.forEach(route => {
@@ -65,10 +77,15 @@ const setupRedirects = () => {
6577
const redirect = `${from} ${to} 200`
6678
console.log(" ", redirect)
6779

68-
redirects.push(redirect)
80+
nextOnNetlifyRedirects.push(redirect)
6981
})
7082
})
7183

84+
// Step 3: Combine redirects into one single array
85+
const redirects = customRedirects
86+
if(nextOnNetlifyRedirects.length)
87+
redirects.push("# Next-on-Netlify Redirects", ...nextOnNetlifyRedirects)
88+
7289
// Write redirects to _redirects file
7390
writeFileSync(
7491
join(NETLIFY_PUBLISH_PATH, "_redirects"),

tests/__snapshots__/defaults.test.js.snap

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,6 @@ exports[`Routing creates Netlify redirects 1`] = `
66
/getServerSideProps/static /.netlify/functions/next_getServerSideProps_static 200
77
/_next/data/%BUILD_ID%/getServerSideProps/static.json /.netlify/functions/next_getServerSideProps_static 200
88
/ /.netlify/functions/next_index 200
9-
/static /static.html 200
10-
/404 /404.html 200
11-
/getStaticProps/static /getStaticProps/static.html 200
12-
/getStaticProps/1 /getStaticProps/1.html 200
13-
/getStaticProps/2 /getStaticProps/2.html 200
14-
/getStaticProps/withFallback/my/path/1 /getStaticProps/withFallback/my/path/1.html 200
15-
/getStaticProps/withFallback/my/path/2 /getStaticProps/withFallback/my/path/2.html 200
16-
/getStaticProps/withFallback/3 /getStaticProps/withFallback/3.html 200
17-
/getStaticProps/withFallback/4 /getStaticProps/withFallback/4.html 200
189
/api/shows/:id /.netlify/functions/next_api_shows_id 200
1910
/api/shows/:params/* /.netlify/functions/next_api_shows_params 200
2011
/getServerSideProps/:id /.netlify/functions/next_getServerSideProps_id 200

tests/__snapshots__/optionalCatchAll.test.js.snap

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
exports[`Routing creates Netlify redirects 1`] = `
44
"# Next-on-Netlify Redirects
5-
/ /index.html 200
6-
/404 /404.html 200
75
/catch* /.netlify/functions/next_catch_all 200
86
/_next/data/%BUILD_ID%/catch* /.netlify/functions/next_catch_all 200"
97
`;

tests/customRedirects.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ describe('Next', () => {
6969
})
7070

7171
describe('Routing',() => {
72-
test('includes custom redirect rules', async () => {
72+
test('includes only custom redirect rules', async () => {
7373
// Read _redirects file
7474
const contents = readFileSync(join(PROJECT_PATH, "out_publish", "_redirects"))
7575

76-
const redirects = contents.toString().split(/\n/)
76+
const redirects = contents.toString().trim().split(/\n/)
7777
expect(redirects[0]).toEqual("# Custom Redirect Rules")
7878
expect(redirects[1]).toEqual("https://old.example.com/* https://new.example.com/:splat 301!")
7979

80-
// Check that other routes are present
81-
expect(redirects).toContain("/ /index.html 200")
80+
// Check that no other redirects are present
81+
expect(redirects).toHaveLength(2)
8282
})
8383
})

tests/preRenderedIndexPages.test.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,12 @@ describe('404 Page', () => {
8989
})
9090

9191
describe('Routing',() => {
92-
test('creates Netlify redirects', async () => {
92+
test('does not create any redirects', async () => {
9393
// Read _redirects file
9494
const contents = readFileSync(join(PROJECT_PATH, "out_publish", "_redirects"))
95+
const redirects = contents.toString()
9596

96-
// Convert contents into an array, each line being one element
97-
const redirects = contents.toString().split("\n")
98-
99-
// Check that routes are present
100-
expect(redirects).toContain("/ /index.html 200")
101-
expect(redirects).toContain("/shows /shows.html 200")
97+
// Check that no redirects are present
98+
expect(redirects).toEqual("")
10299
})
103100
})

tests/staticIndexPages.test.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,15 +89,12 @@ describe('404 Page', () => {
8989
})
9090

9191
describe('Routing',() => {
92-
test('creates Netlify redirects', async () => {
92+
test('does not create any redirects', async () => {
9393
// Read _redirects file
9494
const contents = readFileSync(join(PROJECT_PATH, "out_publish", "_redirects"))
95+
const redirects = contents.toString()
9596

96-
// Convert contents into an array, each line being one element
97-
const redirects = contents.toString().split("\n")
98-
99-
// Check that routes are present
100-
expect(redirects).toContain("/ /index.html 200")
101-
expect(redirects).toContain("/static /static.html 200")
97+
// Check that no redirects are present
98+
expect(redirects).toEqual("")
10299
})
103100
})

0 commit comments

Comments
 (0)