question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting 404 when using localeSubpaths and next-route for dynamic routing

See original GitHub issue

Hi, I am not sure if it is my setup issue or the 2 packages are conflicting…

I have a product.js under pages/ directory I am support 2 languages with these pattern

/product/1
/zh/product/1

However, only the default route (en) works This is my setup

routes.js

const routes = require('next-routes');

module.exports = routes()
    .add({
        name: 'product',
        pattern: '/(zh)?/product/:id',
        page: 'product',
    });

i18n.js

const NextI18Next = require('next-i18next');

module.exports = new NextI18Next({
    otherLanguages: ['zh'],
    defaultLanguage: 'en',
    localeSubpaths: true,
});

server.js

const express = require('express');
const next = require('next');
const nextI18NextMiddleware = require('next-i18next/middleware');

const nextI18next = require('./i18n');
const routes = require('./routes');

const app = next({ dev: process.env.NODE_ENV !== 'production' });
const handler = routes.getRequestHandler(app);
const server = express();

app.prepare()
    .then(() => {
        nextI18NextMiddleware(nextI18next, app, server);
        server
            .use(handler)
            .listen(3000, (err) => {
                if (err) {
                    // eslint-disable-next-line no-console
                    console.error('err', err);
                }
            });
    });

I have read the documentation of both pacakges and tried lots of combination for routes.js, but still can’t make it work. Am I doing this correctly?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:3
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
wk93commented, Dec 25, 2018

I passed next-routes request handler function into next-i18next middleware and I think it works.

(async () => {
  const app = next({ dev: process.env.NODE_ENV !== 'production' })
  const handler = routes.getRequestHandler(app)

  await app.prepare()
  const server = express()

  nextI18NextMiddleware(i18next, { render: handler }, server)

  server.get('*', (req, res) => handler(req, res))

  await server.listen(3000)
  console.log('> Ready on http://localhost:3000')
})()
0reactions
isaachinmancommented, Jan 3, 2019

Closing due to lack of activity, happy to reopen this at any time. I would imagine that @wk93’s approach probably works.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting 404 when using localeSubpaths and next-route for ...
Hi, I am not sure if it is my setup issue or the 2 packages are conflicting.. I have a product.js under pages/...
Read more >
Getting 404 when first loading dynamic routes on nextjs
The real issue is that exporting a next app will make it generate static HTML files. Even though it will still be able...
Read more >
How to return a 404 Not Found page for dynamic routes in ...
For getStaticProps the page will return a 404 even if there was a successfully generated page before. This is meant to support use-cases...
Read more >
Dynamic Routes Details - Next.js
Here is some essential information you should know about dynamic routes. ... then any paths not returned by getStaticPaths will result in a...
Read more >
Eror:404 using Page template for Custom Home Page - Routing
When I'm trying to use default page.hbs as template and create a home page using content from vamsithota.me/home, I'm getting a 404 error....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found