Skip to content

Static exported with getStaticProps and external fetch fails with url's followed by trailing slash and query parameter #9929

@lucaziz

Description

@lucaziz

Bug report

Describe the bug

Using getStaticProps and linking to a local JSON file (exporting static), I notice that if your path has trailing slash plus a query parameter (ex: my.path.com/subpath/?query=1), pathname gets broken and fail to request data.

To Reproduce

  1. Go to a Next page and build using getStaticProps with external fetch (like Static generation https://github.com/zeit/next.js/blob/canary/docs/concepts/_data-fetching.md)
  2. Export project using next export
  3. Navigate to a page with a trailing slash at the end and add a query parameter after that (ex: my.path.com/subpath/?query=1)
  4. See error

Expected behavior

NextJs should handle this path behavior even it's not that common.

System information

  • OS: macOS
  • Browser chrome
  • Version of Next.js: [e.g. 9.1.7-canary.16]

Additional context

Just a small edge case fix.

I know this still a concept, but it looks like is an easy fix. I solved changing (temporary) my local nextjs code at file lib/router/router.js and changed line 63 (inside the function _getStaticData from this:

pathname = !pathname || pathname === '/' ? '/index' : pathname;

to:

pathname = !pathname || pathname === '/' ? '/index' : (pathname.endsWith('/') ? pathname.slice(0,-1) : pathname);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions