Skip to content
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

Questions about Parameterized routing in conjunction with SSR #1532

Closed
afterburn opened this issue Mar 28, 2017 · 4 comments
Closed

Questions about Parameterized routing in conjunction with SSR #1532

afterburn opened this issue Mar 28, 2017 · 4 comments

Comments

@afterburn
Copy link

afterburn commented Mar 28, 2017

I'm using the express version of next.js which I'm using in conjunction with redux.
I've tried several approaches but I'm unclear as to what the best practice in regard to how my router should function is. What I'm trying to accomplish is being able to have my users navigate to /profile/:username and serve a SSR rendered document so it gets picked up by search engine crawlers for SEO purposes.

Ideally I'd like for my application to be able handle route /:username and still have route /about display pages/about.js instead of looking for a user called about, just like facebook does but that's not what this question is about.

This is what my router currently looks like:

const { parse } = require('url');
const pathMatch = require('path-match');
const route = pathMatch();

// Parameterized routes.
const profileRoute = route('/profile/:username');
const newsRoute = route('/news/:article');

((exports) => {
	
	platform.server.get('*', (req, res) => {
		const parsedUrl = parse(req.url, true);
		const { pathname, query } = parsedUrl;
    	
    		// Get parameter(s) if available.
    		let profileParams = profileRoute(pathname);
    		let newsParams = newsRoute(pathname);

    		// Route matching.
		if(pathname.indexOf('/profile/') !== -1) platform.app.render(req, res, '/profile', profileParams);
		else if(pathname === '/news/:article') platform.app.render(req, res, '/news', newsParams);
		else if(pathname === '/about') platform.app.render(req, res, '/about');
		else platform.handle(req, res);		
	});

	console.log("> Routing initialized");

})(module);

I'm guessing that since I'm sending the parameters for let's say user John to be picked up by a component and then have redux fetch the user's data from the back-end is not truly SSR. I also find it inconvenient how I'm currently defining parameterized routes and having to create a variable for each route's parameters. Obviously I don't like having to use indexOf on pathname to detect someone's requesting a profile. Hopefully someone can point out what I could possibly do different.

@afterburn afterburn changed the title Unclear about Parameterized routing + SSR Questions about Parameterized routing in conjunction with SSR Mar 28, 2017
@arunoda
Copy link
Contributor

arunoda commented Mar 29, 2017

I suggest you use express to define routes.

If you wanna handle /:username routes. Do this:

This is the code you can use:
(This is a sample code. Not tested)

// Handle core Next.js routes
['static', '_next', '_webpack', '__webpack_hmr']
  .forEach(function(path) {
    app.get(path, function (req, res) => {
      handle(req, res)
    })
  })

app.get('/about', ...)
app.get('/:user', ...)

@afterburn
Copy link
Author

afterburn commented Mar 31, 2017

Thanks for your reply, I've tried to implement your code and assumed this is what you mean by using express to define routes:

['static', '_next', '_webpack', '__webpack_hmr']
.forEach(function(path) {
	platform.server.get(path, (req, res) => {
		handle(req, res);
	});
});

platform.server.get('/', (req, res) => {
	platform.app.render(req, res, '/');
});

platform.server.get('/about', (req, res) => {
	platform.app.render(req, res, '/about');
});

where platform.server is my express app, and platform.app is my next js instance which might've been poorly chosen names.

This approach however causes the following errors to occur when navigating to http://localhost:3000
e07afdad52d057a27e4d758e37915d57

@fridays
Copy link
Contributor

fridays commented Apr 4, 2017

It should work if you add wildcards to the paths:

['static*', '_next*', '_webpack*', '__webpack_hmr*']

You could also use next-routes where it's done similarly.

@arunoda
Copy link
Contributor

arunoda commented Apr 5, 2017

@fridays thanks for the updated example.

@arunoda arunoda closed this as completed Apr 5, 2017
@lock lock bot locked as resolved and limited conversation to collaborators May 11, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants