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

Commit f2db852

Browse files
committed
_redirects file: Remove path param from Netlify functions
In the past, there was a difference between how Netlify and netlify dev handled redirects. Only on the Netlify platform would the original path be available to the function (e.g. /mypage). Locally, only the function path would be available (e.g. /.netlify/functions/nextRouter). This caused a problem when we want to identify the page to render in nextRouter. Locally, we did not know the original path as requested by the user. To work around that, we passed the page's path as a parameter to the function call. This behavior of netlify dev was fixed in netlify/cli#713
1 parent 883b04e commit f2db852

File tree

4 files changed

+9
-22
lines changed

4 files changed

+9
-22
lines changed

lib/routerTemplate.js

-13
Original file line numberDiff line numberDiff line change
@@ -25,34 +25,21 @@ const getRoute = path => {
2525
}
2626

2727
exports.handler = (event, context, callback) => {
28-
// The following lines set event.path in development environments.
29-
// There is a difference in how Netlify handles redirects locally vs
30-
// production. Locally, event.path is set to the target of the redirect:
31-
// /.netlify/functions/nextRouter?_path=...
32-
// Deployed on Netlify, event.path is the source of the redirect: /posts/3
33-
const isProduction = context.hasOwnProperty('awsRequestId')
34-
if(!isProduction) {
35-
event.path = event.queryStringParameters._path
36-
}
37-
3828
// Get the request URL
3929
const { path } = event
4030
console.log("[request]", path)
4131

42-
4332
// Identify the file to render
4433
const { file } = getRoute(path)
4534
console.log("[render] ", file)
4635

47-
4836
// Load the page to render
4937
// Do not do this: const page = require(`./${file}`)
5038
// Otherwise, Netlify's zip-it-and-ship-it will attempt to bundle "./"
5139
// into the function's zip folder and the build will fail
5240
const pathToFile = `./${file}`
5341
const page = require(pathToFile)
5442

55-
5643
// Render the page
5744
compat(page)(
5845
{

next-on-netlify.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const htmlRedirects = htmlPages.map(({ route, file }) => (
124124
`${route} /_next/${file} 200`
125125
))
126126
const ssrRedirects = ssrPages.map(({ route }) => (
127-
`${route} /.netlify/functions/${ROUTER_FUNCTION_NAME}?_path=${route} 200`
127+
`${route} /.netlify/functions/${ROUTER_FUNCTION_NAME} 200`
128128
))
129129
const nextjsRedirects = [...htmlRedirects, ...ssrRedirects].join("\n")
130130

tests/customNextDistDir.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ describe('Routing',() => {
133133
// Check that routes are present
134134
expect(redirects).toContain("/static /_next/pages/static.html 200")
135135
expect(redirects).toContain("/static/:id /_next/pages/static/[id].html 200")
136-
expect(redirects).toContain("/ /.netlify/functions/nextRouter?_path=/ 200")
137-
expect(redirects).toContain("/index /.netlify/functions/nextRouter?_path=/index 200")
138-
expect(redirects).toContain("/shows/:id /.netlify/functions/nextRouter?_path=/shows/:id 200")
139-
expect(redirects).toContain("/shows/* /.netlify/functions/nextRouter?_path=/shows/* 200")
136+
expect(redirects).toContain("/ /.netlify/functions/nextRouter 200")
137+
expect(redirects).toContain("/index /.netlify/functions/nextRouter 200")
138+
expect(redirects).toContain("/shows/:id /.netlify/functions/nextRouter 200")
139+
expect(redirects).toContain("/shows/* /.netlify/functions/nextRouter 200")
140140
})
141141
})

tests/defaults.test.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ describe('Routing',() => {
133133
// Check that routes are present
134134
expect(redirects).toContain("/static /_next/pages/static.html 200")
135135
expect(redirects).toContain("/static/:id /_next/pages/static/[id].html 200")
136-
expect(redirects).toContain("/ /.netlify/functions/nextRouter?_path=/ 200")
137-
expect(redirects).toContain("/index /.netlify/functions/nextRouter?_path=/index 200")
138-
expect(redirects).toContain("/shows/:id /.netlify/functions/nextRouter?_path=/shows/:id 200")
139-
expect(redirects).toContain("/shows/* /.netlify/functions/nextRouter?_path=/shows/* 200")
136+
expect(redirects).toContain("/ /.netlify/functions/nextRouter 200")
137+
expect(redirects).toContain("/index /.netlify/functions/nextRouter 200")
138+
expect(redirects).toContain("/shows/:id /.netlify/functions/nextRouter 200")
139+
expect(redirects).toContain("/shows/* /.netlify/functions/nextRouter 200")
140140
})
141141
})

0 commit comments

Comments
 (0)