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

fix(router): fix URL creation in Cloudflare Pages #9682

Merged
merged 5 commits into from
Dec 5, 2022

Conversation

brophdawg11
Copy link
Contributor

We ran into an issue in Remix 1.8.0 where the static handler was failing to create a new URL() in Cloudflare pages. This is because we ended up trying to share some code to create URL instances that should have been client-only in the static handler. This PR updates our code to prefer creating URLs from Requests (which will always have origins) where possible via new URL(request.url) and makes it more explicit where we need to create client-side URL/Request instances.

Closes remix-run/remix#4740

@changeset-bot
Copy link

changeset-bot bot commented Dec 5, 2022

🦋 Changeset detected

Latest commit: d518c72

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 5 packages
Name Type
@remix-run/router Patch
react-router Patch
react-router-dom Patch
react-router-dom-v5-compat Patch
react-router-native Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

if (value === false || value === null || typeof value === "undefined") {
throw new Error(message);
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved from utils.ts to here to avoid a circular dependency

@MichaelDeBoey MichaelDeBoey changed the title fix URL creation in cloudflare pages fix(router): fix URL creation in Cloudflare Pages Dec 5, 2022
@@ -544,7 +558,7 @@ export function parsePath(path: string): Partial<Path> {
return parsedPath;
}

export function createURL(location: Location | string): URL {
export function createClientSideURL(location: Location | string): URL {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this more apparent that it's not intended to be used in staticHandler

@@ -954,7 +954,7 @@ export function createRouter(init: RouterInit): Router {
loadingNavigation = navigation;

// Create a GET request for the loaders
request = createRequest(request.url, request.signal);
request = new Request(request.url, { signal: request.signal });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we already have a valid request, we can just inline the request creation. createClientSideRequest is now for when we don't have a request and we just have a path

Comment on lines -2627 to +2634
let external = createURL(location).origin !== createURL("/").origin;
let currentUrl = new URL(request.url);
let currentOrigin = currentUrl.origin;
let newOrigin = new URL(location, currentOrigin).origin;
let external = newOrigin !== currentOrigin;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the main fix for cloudflare - we inline the URL creation and just use the current request to determine the origin

Comment on lines -556 to 576
: "unknown://unknown";
: window.location.href;
let href = typeof location === "string" ? location : createPath(location);
invariant(
base,
`No window.location.(origin|href) available to create URL for href: ${href}`
);
return new URL(href, base);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The unknown://unknown was a bit of a snap decision very early on, but I think was really a red herring in some unit testing setup issues since new URL('/', 'unknown://unknown') doesn't actually work quite right anyway! Now that we've better defined these APIs as intended only for client-side use - we can just use invariant if we can't detect a proper base.

For the specific Firefox file:// case, unknown://unknown wasn't actually working right and gave incorrect paths - but it didn't throw an error. Instead, firefox does have a "valid" window.location.href we can use as a fallback base when origin = "null" (in file:// mode):

Screenshot 2022-12-05 at 4 21 11 PM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants