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

Trailing slash causes redirect loop #7529

Open
1 task done
joelvh opened this issue Sep 25, 2023 · 4 comments · Fixed by #9695
Open
1 task done

Trailing slash causes redirect loop #7529

joelvh opened this issue Sep 25, 2023 · 4 comments · Fixed by #9695

Comments

@joelvh
Copy link

joelvh commented Sep 25, 2023

What version of Remix are you using?

2.0.0

Are all your remix dependencies & dev-dependencies using the same version?

  • Yes

Steps to Reproduce

  1. Create a page, ideally a nested path such as app/routes/folder.file._index.tsx (URL path /folder/file)
  2. Set up a reverse proxy (e.g. Shopify App Proxy) pointing to that path (e.g. https://www.example.com/folder/file)
  3. Update remix.config.js to set publicPath = "/folder/file/build/ so assets link through the reverse proxy
  4. Visit the proxy path (e.g. https://example.myshopify.com/folder/file) which reverse proxies to www.example.com
  5. Observe: browser path does not have a trailing slash, but the request from the reverse proxy to the Remix app has a trailing slash (e.g https://www.example.com/folder/file/)
  6. Observe: browser reloads infinitely with error message in browser console that says Initial URL (/folder/file/) does not match URL at time of hydration (/folder/file), reloading page...

Expected Behavior

Expect the refresh behavior to realize that a trailing slash should be ignored.

Temporary fix is to place the following in entry.client.tsx:

  const initialPathname = window.__remixContext.url
  const hydratedPathname = window.location.pathname

  /**
   * Fix initial pathname to match browser if we're dealing with trailing slashes.
   */
  if (
    initialPathname !== hydratedPathname &&
    initialPathname.replace(/\/+$/, '') === hydratedPathname.replace(/\/+$/, '')
  ) {
    window.__remixContext.url = hydratedPathname
  }

Actual Behavior

Refresh doesn't realize the two paths are effectively the same.

UPDATE: It turns out this is also an issue with links and causes the inverse issue. The Link will have a path such as /folder/file and the path in the browser becomes /folder/file/, which I believe is updated by the router. I've updated the script that fixes this (above). I've also updated the steps to reproduce to call out that we are using a custom publicPath.

@brophdawg11
Copy link
Contributor

This should be resolved via #9695 and available in the next release 👍

@brophdawg11 brophdawg11 added the awaiting release This issue has been fixed and will be released soon label Jul 24, 2024
@brophdawg11 brophdawg11 removed their assignment Jul 24, 2024
Copy link
Contributor

🤖 Hello there,

We just published version 2.11.0-pre.0 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

Copy link
Contributor

github-actions bot commented Aug 1, 2024

🤖 Hello there,

We just published version 2.11.0 which involves this issue. If you'd like to take it for a test run please try it out and let us know what you think!

Thanks!

@brophdawg11
Copy link
Contributor

We had to revert the fix in #9695 so re-opening this. We should be able to remove this check entirely instead in #9890

@brophdawg11 brophdawg11 reopened this Aug 19, 2024
@brophdawg11 brophdawg11 removed the awaiting release This issue has been fixed and will be released soon label Aug 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants