Make server respond with 404 status on unmatched URLs

If you’re doing server side rendering (SSR), it looks like React-router can handle this quite well: https://reacttraining.com/react-router/web/guides/server-rendering/

Integrated with Meteor’s server-render package would look something like this:

import React from 'react';

import { renderToString } from 'react-dom/server';
import { onPageLoad } from 'meteor/server-render';
import { StaticRouter } from 'react-router';

import App from './App';

onPageLoad(sink => {
    const context = {};

    sink.renderIntoElementById(
        'app', // Assuming you're rendering into an element where id="app"
        renderToString(
            <StaticRouter location={sink.request.url} context={context}>
                <App />
            </StaticRouter>
        )
    );
    if (context.status) sink.setStatusCode(context.status);
    if (context.url) sink.redirect(context.url);
});

And App would use something like the Status and NotFound components on the page linked above


Unfortunately the docs for server-render don’t explain that you can use sink.setStatusCode, but the github page has it: https://github.com/meteor/meteor/tree/master/packages/server-render