Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<Link as={...}> causes full refresh if as parameter is a URL that can be rewritten #22788

Closed
ghost opened this issue Mar 5, 2021 · 5 comments
Labels
bug Issue was opened via the bug report template. please add a complete reproduction The issue lacks information for further investigation Stale

Comments

@ghost
Copy link

ghost commented Mar 5, 2021

What version of Next.js are you using?

10.0.7

What version of Node.js are you using?

14.10.1

What browser are you using?

Chrome

What operating system are you using?

Windows

How are you deploying your application?

next start

Describe the Bug

Following the advice in #11625, it doesn't work if the as={...} parameter uses a dynamic route.

Expected Behavior

A full page load should not occur. The URL should change but the user should not be navigated away from the page.

To Reproduce

In next.config.js:

        rewrites: async () => {
            return [
                {
                    source: '/@:username',
                    destination: '/users/:username'
                },
                {
                    source: '/@:username/:path*',
                    destination: '/users/:username/:path*'
                }
            ];
        },

Then, create a link of the form:

<Link href={`/?postId=123`} as={`/@foo/posts/123`} />

This is triggered in next-server/lib/router/router.ts, under if (process.env.__NEXT_HAS_REWRITES && asPath.startsWith('/')) {:

      if (rewritesResult.matchedPage && rewritesResult.resolvedHref) {
        // if this directly matches a page we need to update the href to
        // allow the correct page chunk to be loaded
        route = rewritesResult.resolvedHref
        pathname = rewritesResult.resolvedHref
        parsed.pathname = pathname
        url = formatWithValidation(parsed)
        resolvedAs = rewritesResult.asPath
      }

If a rewrite is matched, it will throw away the passed in URL and replace it with the rewrite result. This will then cause a full page navigation which does not occur if as was not set to a URL that is subject to rewrite.

@ghost ghost added the bug Issue was opened via the bug report template. label Mar 5, 2021
@ghost ghost changed the title <Link as={...}> causes full refresh if as parameter is a rewrite <Link as={...}> causes full refresh if as parameter is a URL that can be rewritten Mar 5, 2021
@ghost
Copy link
Author

ghost commented Mar 11, 2021

Oops, I see this is the same issue as #21410.

@ghost ghost closed this as completed Mar 11, 2021
@ghost
Copy link
Author

ghost commented Mar 11, 2021

Hm actually this might be a different issue: it still seems broken in 10.0.9-canary.5.

@ghost ghost reopened this Mar 11, 2021
@jankaifer
Copy link
Contributor

Could you provide a reproduction?
Ideally a repo I can clone and run.

We made a lot of changes to next/link so it might be fixed in next@canary.

@jankaifer jankaifer added the please add a complete reproduction The issue lacks information for further investigation label Nov 25, 2022
@balazsorban44
Copy link
Member

This issue has been automatically closed because it received no activity for a month and had no reproduction to investigate. If you think it was closed by accident, please leave a comment. If you are running into a similar issue, please open a new issue with a reproduction. Thank you.

@github-actions
Copy link
Contributor

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue was opened via the bug report template. please add a complete reproduction The issue lacks information for further investigation Stale
Projects
None yet
Development

No branches or pull requests

2 participants