Next.js: 9.1.5 path-to-regexp error with static routes with special symbols * ( +

Created on 11 Dec 2019  路  6Comments  路  Source: vercel/next.js

Bug report

if you place files in public folder with symbols * ( ) + and run node server.js in production you will get something like

/node_modules/next/node_modules/path-to-regexp/dist/index.js:80
                throw new TypeError("Unbalanced pattern at " + i);
                TypeError: Unexpected MODIFIER at 1, expected END

or

/node_modules/next/node_modules/path-to-regexp/dist/index.js:113
        throw new TypeError("Unexpected " + nextType + " at " + index + ", expected " + type);
        ^

Describe the bug

This is bug with path-to-regexp. And it happens only in 9.1.5 after upgrade path-to-regexp from version 3 to 6.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

1) npx create-next-app --example custom-server-express custom-server-express-app
or
yarn create next-app --example custom-server-express custom-server-express-app
2) Create public folder
3) Create any file with any symbol from * or ( or + . For example logo18+.png
4) Put that file in public folder
5) Run NODE_ENV=production node server.js

Expected behavior

Route created for static svg -> server started

System information

  • OS: MacOS 10.15
  • Version of Next.js: 9.1.5
bug

Most helpful comment

@markotom you should change usage of * in routes to (.*) or :splat* as per koajs/router v9 changelog.

All 6 comments

Fixed in 9.1.8-canary.11 -- please give it a try!

Solved my issue. Thanks @Timer.

It's fixed in 9.2.0 (canary was released)

I am using 9.3.6 and am seeing this when using + in routes for structures like this: /+/[slug]/index.js

same problem using koa line router.get('*', ...:

const Koa = require('koa');
const Router = require('koa-router');
const next = require('next');

const server = new Koa();
const router = new Router();

const dev = process.env.NODE_ENV !== 'production';
const app = next({ dev });
const handle = app.getRequestHandler();

async function init() {
  try {
    await app.prepare();

    router.get('/', async (ctx) => {
      await app.render(ctx.req, ctx.res, '/home', ctx.query);
      ctx.respond = false;
    });

    router.get('*', async (ctx) => {
      await handle(ctx.req, ctx.res);
      ctx.respond = false;
    });

    server.use(router.routes());
    server.listen(process.env.APP_PORT, () =>
      console.info(`> Ready on ${process.env.APP_HOST} 馃殌`),
    );
  } catch (err) {
    console.error(err);
  }
}

init();

@markotom you should change usage of * in routes to (.*) or :splat* as per koajs/router v9 changelog.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

swrdfish picture swrdfish  路  3Comments

havefive picture havefive  路  3Comments

renatorib picture renatorib  路  3Comments

rauchg picture rauchg  路  3Comments