As stated here on Spectrum, I'm having troubles in migrating my Next website from an Express custom server to Now. My problem is that I'm unable to get the home page, because I'm trying to send URL params, with no success. I need to pass a lang
parameter, as follows:
localhost:3000/en
Then, I would have to get the Home as expected. But I'm receiving this exception:
TypeError: Cannot read property 'lang' of undefined at Function.getInitialProps (/Users/lucacattide/Vagrant/debian/public/LC/front-end/.next/server/static/development/pages/_document.js:1111:32)
That's because I was previously reading the ctx.req.params.lang
parameter from _document.js
getInitialProps
for various reasons. Even by replacing it with ctx.req.query.lang
I'm still getting it.
I'm currently copying two examples both by Zeit migration guide and article. As both suggest, I'm trying the API
approach (https://zeit.co/guides/migrate-to-zeit-now/) and the pages
one too (https://zeit.co/guides/custom-next-js-server-to-routes/).
In the API one, I've implemented inside /api
path, the [lang].js
segment, with the following content:
module.exports = (req, res) => {
res.send(req.query.lang);
}
Then with the /pages
one, I copied and renamed the index.js
to [lang].js
inside the same path, and replaced the req.query.lang
as mentioned above. Both of these doesn't work. I'm stuck with the same exception, that warns about _document.js
. I need that because of different processes inside it, so deleting it isn't an option in my case.
Of course I'm testing on Now environment (running now dev
) on my localhost
. The app works perfectly on Express, so I hope to solve this issue in order to deploy it correctly on Now.
Anyone experienced this before or could help me with some suggestion?
Thanks in advance for support.
EDIT:
I tried to force paths too by defining routes in now.json
but the result is the same. Here my current configuration:
{
"public": false,
"name": "LC",
"version": 2,
"routes": [{
"src": "/(?<lang>[^/]+)",
"dest": "/?lang=$lang"
}]
}
EDIT 2:
I switched all inside pages
dir as suggested from 2nd guide, because I wasn't getting nothing from /api
. Trying by placing only a [lang].js
route and removing routes definitions from now.json
.
At this moment, by inspecting the req
object I receive an empty one.
Due to Now platform updates - v2 - I noticed that a lot of Next API parts has changed. By revamping my code in order to following the new approaches, I'm able to get the previously missing parameter. In details, I followed the API routes tips on the latest official documentation
Thanks everyone for the help.