Hey,
have you anyone tried this solution with custom server and get it working?
Example:
// server.js
const express = require('express');
const next = require('next');
const port = parseInt(process.env.PORT, 10) || 3000;
const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
server.get('/blog/:id', (req, res) => {
console.log('My params needed be passed to page:', req.params);
return app.render(req, res, '/blogDetail', { id: req.params.id });
});
server.listen(port, err => {
if (err) throw err;
console.log(`> Ready on http://localhost:${port}`);
});
});
// blogDetail.js
export async function unstable_getStaticProps(props) {
console.log('What is passed', props);
return {};
}
const BlogPostPage = ({ post }) => {
return <div>Hey</div>;
}
export default BlogPostPage;
# Terminal output
My params needed be passed to page: { id: 'test' }
What is passed { params: undefined }
_Originally posted by @homoky in https://github.com/zeit/next.js/issues/9524#issuecomment-573640558_
Thanks for bringing this to our attention!
getStaticProps currently only supports params coming from Next.js' built-in dynamic routes (sourced via getStaticPaths).
I'm not sure if we're going to change this.
In this example, you can completely eliminate your custom server and rename your page from pages/blogDetail.js to pages/blog/[id].js.
Actually, in this exact example the page is even being rendered on-demand and not static as it should be. I'm going to close this as a non-goal of the API design.
Thank you for your explanation.
I would like to quickly reply:
Actually, in this exact example the page is even being rendered on-demand and not static as it should be.
Yeah I am aware of it, I wanted to make sure it does support it in the dev mode. If this method is not supported the second one for getting static paths will not be as well.
Thanks
@Timer So there is no change to get this working with, for example, Firebase cloud functions?
@HofmannZ @homoky seems that you're both misunderstanding what getStaticProps does , it renders the provided paths from getStaticPaths to static HTML at build time. The way that you provided the example you'd want to use getServerProps which renders on-demand.
@timneutkens I've misread the title, we're trying to use unstable_getServerProps but does not seem to work with a custom server. Is that something you're aware of? Would love to contribute some test cases of scenarios we have in mind where this could be useful.
@HofmannZ it's not implemented yet, keep in mind that you're using experimental features.
Relevant PR: https://github.com/zeit/next.js/pull/10077
Thanks for bringing this to our attention!
getStaticPropscurrently only supports params coming from Next.js' built-in dynamic routes (sourced viagetStaticPaths).I'm not sure if we're going to change this.
In this example, you can completely eliminate your custom server and rename your page from
pages/blogDetail.jstopages/blog/[id].js.
@Timer, @timneutkens I'm just wondering whether there is any plan to support getStaticProps with a custom server yet?
Just to give some context as to why I ask. We're currently building an ecommerce front end and know our clients would not want '/category/[...slug]' or '/product/[...slug]' as they require full control over their URLs.
Ideally we'd like to be able to offer static/pre rendered pages especially with the new 'Incremental Static Regeneration' in the pipeline but are currently unable to do so.
If getStaticProps with custom servers is not in the pipeline can you think of any suggestions that might solve the issue?
Most helpful comment
@HofmannZ it's not implemented yet, keep in mind that you're using experimental features.
Relevant PR: https://github.com/zeit/next.js/pull/10077