Skip to content

using Pages Router with basePath set in next.config.js results in 404 error when making <basePath>/_next/data/<BUILD_ID>/<path>.json requests #897

Open
@chiakaiming

Description

@chiakaiming

Hello!

I'm consistently facing a 404 error on <basePath>/_next/data/<BUILD_ID>/<path>.json requests.

The issue arises when providing a value for basePath in next.config.js (tested on a project using pages router).

Looking at the matchRoute function in packages/open-next/src/core/routing/routeMatcher.ts, the regex array used to match a path is derived from optionalBasepathPrefixRegex which prepends the basePath to the path regex. This results is a regex pattern that looks something this:

/^\/basePath\/?(?:)?path(?:\/)?$/

In the function fixDataPage in packages/open-next/src/core/routing/matcher.ts, we are removing the dataPattern (<basePath>/_next/data/<BUILD_ID>) from rawPath which includes the basePath resulting in a transformed rawPath of just /path.

When matchRoute is eventually called, it will try to match the above regex pattern with /path which will fail, resulting in a 404 response return for the data route.

When newPath is initialised in fixDataPage, can we prepend it with ${NextConfig.basePath ?? ""} so that the transformed rawPath would be basePath/path?

Thank you!

PS: Currently, when handling a route, fixDataPage outputs /path for both .../basePath/.../path and .../path requests which I don't think is the intended behaviour?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions