Using next.js app deployed on firebase.
I have deployed the app already and using some time.
- I created a dynamic route page
- Deployed again app
- I could access the dynamic page via a router, but once I refresh the page I got a 404 page not found
Solutions that I tried and didn’t work:
- Rewrites with dynamicLink true
- Rewrites with destination to the dynamic route and regex / source
"rewrites": [
{
"source": "/job/**",
"destination": "/job/[jobId]/index.html"
}
- cleanUrls true
- trailingSlash: true plus rewrite config with destination to the dynamic page
- Any many more
I found a ton of solutions for this problem but no one didn’t work for me.
Any idea?
firebase.json file:
{
"hosting": {
"public": "out",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"cleanUrls": true,
"rewrites": [
{
"source": "**",
"destination": "out/index.html"
}
]
},
"functions": [
{
"source": "functions",
"codebase": "default",
"ignore": [
"node_modules",
".git",
"firebase-debug.log",
"firebase-debug.*.log"
],
"predeploy": ["npm --prefix "$RESOURCE_DIR" run build"]
}
]
}
3
Answers
This firebase.json works for me.
For dynamic routes to work, you have to use server functions. You can use experimental frameworks feature by Firebase.
Checkout – https://firebase.google.com/docs/hosting/nextjs
Next.js advanced (dynamic) routing does not work without server functions.
Follow these steps:
firebase.json
and.firebase
etc. And update firebase cli.firebase experiments:enable webframeworks
firebase init hosting
in the root directory of your Next.js app and follow the prompts.firebase deploy
Option 2: working example
Try following
Checkout this repo for working example – working example
Try navigating to https://byte-ceps.web.app/aboutOrAnything
Improving UX
move your landing page to
/home
Option 3:
Deploy on vercel.com
Simply create a vercel account and link it to your GitHub. Create project, select your GitHub repo and setup any secrets if you need to. You are done.
If you want to have your .web.app domain, you need to either redirect to vercel deployment from your firebase site or use iframe. But if you have already purchased your own domain name, you can simply setup cname entry and your deployment is ready.
Update
Also in your
firebase.json
shared in question, rewrite should be to/index.html
and notout/index.html
To properly handle dynamic routes in Firebase hosting, you need to set
cleanUrls: false
in yourfirebase.json
file and add a rewrite rule to handle all requests toindex.html
:The following will handle dynamic request correctly, however if you refresh it will take you to to the home page.
You can fix this issue by adding a static directory rule to your firebase.json file:
Full code: