-
Notifications
You must be signed in to change notification settings - Fork 26.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Dev server does not serve routes expected to be built by static export #2823
Comments
I'm not familiar enough (yet) with the source, but if it's any help to someone who is, my guess is that this could be easily fixed by adding the result of |
Hey @tannerlinsley, what I did on my team is extract the routes into its own module that just returns an object with the route as the key and data about the route (e.g., page and query) as the value. We then consume this module in |
Yeah, we've tried almost that exact same thing, though we don't like relying on a custom server, given we are only using next to export static sites. Would love to see your code regardless :) |
Hey @tannerlinsley, sorry for the delay. We have three files: routes.js: module.exports = () => {
return {
'/': { page: '/' },
'/404': { page: '/404' },
'/about': { page: '/about' },
...
}
} developmentServer.js: const express = require('express');
const next = require('next');
const { parse } = require('url');
const DEV = process.env.ENVIRONMENT !== 'production';
const PORT = 4567;
const app = next({dir: '.', dev: DEV});
const handle = app.getRequestHandler();
const getRoutes = require('lib/routes');
const routes = getRoutes();
app.prepare().then(() => {
const server = express();
server.get('*', (req, res) => {
const parsedUrl = parse(req.url, true);
const { pathname, query } = parsedUrl;
const route = routes[pathname];
if (route) {
return app.render(req, res, route.page, route.query);
}
return handle(req, res);
});
server.listen(PORT, (err) => {
if (err) throw err;
console.log(`> READY FOR LIFOTFF http://localhost:${PORT}`);
});
}); and finally, next.config.js: const getRoutes = require('lib/routes');
module.exports = {
exportPathMap: getRoutes
}; Let me know if you have any questions (or suggestions!). |
This works perfectly. Thanks guys. |
@ldthorne I guess it'd be good to document this in the wiki 👌 |
Thanks for this solution @ldthorne. I just have a little issue about it; I have a dynamic link like Is it a way to redirect to a 404 without having theses errors? Thanks and cool for adding this solution on the wiki 🤓 |
Expected Behavior
In dev mode, navigating to
/blog/posts/my-post-title
should be picked up by the router as a destination that will eventually be available on static export.Current Behavior
In dev mode, navigating to
/blog/posts/my-post-title
is not be picked up by the router, and you get a 404 where a static HTML file would normally be located after exporting the static site.Steps to Reproduce (for bugs)
next.config.js
:next
localhost:3000/p/hello-nextjs
Context
The expected behavior is extremely relevant to using Next to build static websites. One key component to developing a static website is the ability for the local development server to resemble as closely as possible the conditions where the static site will be served. In this case, I would expect that the paths exported from
exportPathMap
would be used to serve the correct page.You may be thinking that
next-routes
+<Link as='...' />
is a good solution to this problem, where the browser push state masks navigation to/blog/posts/my-post-title
with something likeblog-post?slug=my-post-title
, but this only works when navigating from a root page or index, not when reloading or opening a new window.The text was updated successfully, but these errors were encountered: