fridays / next-routes

Universal dynamic routes for Next.js
MIT License
2.47k stars 230 forks source link

Improve Documentation (How to setup) #184

Closed yves-s closed 6 years ago

yves-s commented 6 years ago

In general it's pretty well documented but the last steps seem to be missing to get it up and running.

Under "How to use" you describe the routes.js file setup.

const routes = module.exports = require('next-routes')()

routes
.add('about')
.add('blog', '/blog/:slug')
.add('user', '/user/:id', 'profile')
.add('/:noname/:lang(en|es)/:wow+', 'complex')
.add({name: 'beta', pattern: '/v3', page: 'v3'})

But you don't explain how and what to export.

In the section "On the server" there is routes imported/required but we didn't export anything in routes.js.

...
const routes = require('./routes')
...

If I do every step as explained and export routes from routes.js as:

// routes.js
...
module.exports = routes;

I still get an 404 after refresh and my node server (express) doesn't find my defined routes.

Also you explain in "On the client", Link should also be used from routes.js but we didn't export that either.

Maybe an example would help as well.

yves-s commented 6 years ago

Ok, I got it:

// routes.js
const routes = module.exports = require('next-routes')(); 

routes 
    .add('p', '/p/:slug', 'product') 
    .add('c', '/c/:slug', 'content') 
    .add('service', '/service/:slug') 
    .add('legal', '/legal/:slug'); 

module.exports.Link = routes.Link; 
module.exports.Router = routes.Router; 
module.exports = routes; 

and I had to add/remove

// server.js
...
- const handle = app.getRequestHandler(); 
+ const handler = routes.getRequestHandler(app); 
...
- server.get('*', (req, res) => { 
-     return handle(req, res) 
- }); 
server 
+    .use(handler) 
    .listen(3000, (err) => { 
        if(err) throw err; 
        console.log('> Ready on http://localhost:3000'); 
     }); 
michaeljonathanblack commented 6 years ago

This line const routes = module.exports = require('next-routes')(); is an export. You can see module.exports getting assigned right there.

chrisehlee commented 6 years ago

I'm having issues with this too .. external links to the app throw error 404. Don't know what I'm doing wrong

fridays commented 6 years ago

@mherodev that's right!

@chrisehlee If your getting 404 please check if your starting the app by running node server.js (instead of next start)