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.

Using next-routes with prefix

See original GitHub issue

Hi I have an issue like below:

server.js

app.prepare().then(() => {
  const server = express();
  const router = express.Router();

  router.get('/:area', (req, res) => {
    return app.render(req, res, '/index', req.query);
  });

  router.get('*', (req, res) => handle(wrappedRequest(req), res));

  server.use('/prefix', router);
  server.use('/prefix/static', express.static('static'));

  server.listen(3000, err => {
    if (err)
      throw err;
    console.log(`> Ready on http://localhost:3000/prefix`);
  });
});

what I want to do is using next-routes? It worked fine when not using prefix.

Is there any way to integrate prefix to next-route?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

8reactions
stanleyfokcommented, Apr 22, 2018

My solution is to modify the findAndGetUrls method. I am using the ENV var named basedPath. It works nicely

next.config.js

module.exports = {
  assetPrefix: envConfig.ASSET_PREFIX,

  publicRuntimeConfig: {
    basePath: envConfig.BASE_PATH,
  },
}

server.js

...
          const server = express();
          const router = express.Router();

          router.get('*', (req, res) => handle(req, res));

          server.use(envConfig.BASE_PATH, router);

          server.listen(envConfig.PORT, (err) => {
            if (err) throw err;

            console.log(`> Ready on http://localhost:${envConfig.PORT}`);
          });
...

routes.js

const escapeRegExp = require('lodash/escapeRegExp');
const config = require('next/config').default();

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

const oldFindAndGetUrls = routes.findAndGetUrls.bind(routes);

routes.findAndGetUrls = (nameOrUrl, params) => {
  const { basePath } = config.publicRuntimeConfig;

  let mNameOrUrl = nameOrUrl;
  if (basePath && basePath !== '/') {
    const re = new RegExp(`^${escapeRegExp(basePath)}`);
    mNameOrUrl = nameOrUrl.replace(re, '');
  }

  const findAndGetUrls = oldFindAndGetUrls(mNameOrUrl, params);

  if (basePath && basePath !== '/') {
    findAndGetUrls.urls.as = `${basePath}${findAndGetUrls.urls.as}`;
  }

  return findAndGetUrls;
};

// usage: routes.add(name, pattern, page);
routes
  .add('index', '/', 'index')
  .add('login', '/login', 'login');

module.exports = routes;
4reactions
zvictorcommented, Apr 12, 2018

I wanted to have all my routes prefixed with assetPrefix, but I didn’t manage to export it from the config file in a clean way.

My solution was to replicate assetPrefix in the public settings and override Link with it:

next.config.js

const PREFIX = '/myAppPrefix'

module.exports = {
  assetPrefix: PREFIX,
  publicRuntimeConfig: {
    assetPrefix: PREFIX,
  },
}

routes.js

const { mapProps } = require('recompose')
const NextLink = require('next/link').default
const config = require('next/config').default()

const mapper = ({ as, href, ...props }) => {
  const { assetPrefix } = config.publicRuntimeConfig

  return {
    ...props,
    as: `${assetPrefix}${as}`,
    href: `${assetPrefix}${href}`,
  }
}

const Link = mapProps(mapper)(NextLink)
const routes = (module.exports = require('next-routes')({ Link }))

routes.add('home', '/', 'index')
...
Read more comments on GitHub >

github_iconTop Results From Across the Web

next.js - Adding prefix to Nextjs dynamic route - Stack Overflow
This will make any link to /@username go to the /users/[username] file, even though the address bar will show /@username . Then, in...
Read more >
Routing: Introduction - Next.js
Next.js has a built-in, opinionated, and file-system based Router. ... The router will automatically route files named index to the root of the...
Read more >
next-routes-with-locale - npm
Easy to use locale-based dynamic routes for Next.js. ... Start using next-routes-with-locale in your project by running `npm i ...
Read more >
next-routes-i18n - npm Package Health Analysis - Snyk
next-routes -i18n. v1.8.6. Internationalized routing alternative for Next.js For more information about how to use this package see README.
Read more >
Route.Chain (jooby-project 1.6.6 API)
All the pending/next routes from pipeline. Method Detail. next. void next(@Nullable String prefix, Request req, ...
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