next.js icon indicating copy to clipboard operation
next.js copied to clipboard

9.1.5 path-to-regexp error with static routes with special symbols * ( +

Open ERRORthenBSOD opened this issue 6 years ago • 7 comments

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

ERRORthenBSOD avatar Dec 11 '19 15:12 ERRORthenBSOD

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

Timer avatar Jan 10 '20 02:01 Timer

Solved my issue. Thanks @Timer.

vegetableman avatar Jan 27 '20 23:01 vegetableman

It's fixed in 9.2.0 (canary was released)

timneutkens avatar Jan 28 '20 09:01 timneutkens

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

panbanda avatar May 04 '20 08:05 panbanda

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 avatar Jul 01 '20 19:07 markotom

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

evenfrost avatar Jul 06 '20 16:07 evenfrost

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

balazsorban44 avatar Jan 29 '22 23:01 balazsorban44