Skip to content

Questions about Parameterized routing in conjunction with SSR #1532

@afterburn

Description

@afterburn

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions