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

Hotfix: absolute URLs on server router #10112

Merged
merged 6 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/lovely-cheetahs-sip.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"react-router-dom": patch
---

Fix SSR of absolute Link urls
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
- tanayv
- theostavrides
- thisiskartik
- thomasverleye
- ThornWu
- timdorr
- TkDodo
Expand Down
42 changes: 42 additions & 0 deletions packages/react-router-dom/__tests__/data-static-router-test.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/**
* @jest-environment node
*/

import * as React from "react";
import * as ReactDOMServer from "react-dom/server";
import type { StaticHandlerContext } from "@remix-run/router";
Expand Down Expand Up @@ -611,6 +615,44 @@ describe("A <StaticRouterProvider>", () => {
`);
});

it("renders absolute links correctly", async () => {
let routes = [
{
path: "/",
element: (
<>
<Link to="/the/path">relative path</Link>
<Link to="http://localhost/the/path">absolute same-origin url</Link>
<Link to="https://remix.run">absolute different-origin url</Link>
<Link to="mailto:foo@baz.com">absolute mailto: url</Link>
</>
),
},
];
let { query } = createStaticHandler(routes);

let context = (await query(
new Request("http://localhost/", {
signal: new AbortController().signal,
})
)) as StaticHandlerContext;

let html = ReactDOMServer.renderToStaticMarkup(
<React.StrictMode>
<StaticRouterProvider
router={createStaticRouter(routes, context)}
context={context}
/>
</React.StrictMode>
);
expect(html).toMatch(
'<a href="/the/path">relative path</a>' +
'<a href="http://localhost/the/path">absolute same-origin url</a>' +
'<a href="https://remix.run">absolute different-origin url</a>' +
'<a href="mailto:foo@baz.com">absolute mailto: url</a>'
);
});

describe("boundary tracking", () => {
it("tracks the deepest boundary during render", async () => {
let routes = [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// Polyfill jsdom SubmitEvent.submitter, until https://github.com/jsdom/jsdom/pull/3481 is merged
if (
typeof SubmitEvent === "undefined" ||
!SubmitEvent.prototype.hasOwnProperty("submitter")
typeof window !== "undefined" &&
(typeof SubmitEvent === "undefined" ||
!SubmitEvent.prototype.hasOwnProperty("submitter"))
) {
const setImmediate = (fn, ...args) => global.setTimeout(fn, 0, ...args);

Expand Down
3 changes: 2 additions & 1 deletion packages/react-router-dom/__tests__/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
TextEncoder as NodeTextEncoder,
TextDecoder as NodeTextDecoder,
} from "util";
import { fetch, Request, Response } from "@remix-run/web-fetch";
import { fetch, Request, Response, Headers } from "@remix-run/web-fetch";
import { AbortController as NodeAbortController } from "abort-controller";

import "./polyfills/SubmitEvent.submitter";
Expand All @@ -22,6 +22,7 @@ if (!globalThis.fetch) {
// web-std/fetch Response does not currently implement Response.error()
// @ts-expect-error
globalThis.Response = Response;
globalThis.Headers = Headers;
}

if (!globalThis.AbortController) {
Expand Down
31 changes: 17 additions & 14 deletions packages/react-router-dom/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ const isBrowser =
typeof window.document !== "undefined" &&
typeof window.document.createElement !== "undefined";

const ABSOLUTE_URL_REGEX = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i;

/**
* The public API for rendering a history-aware <a>.
*/
Expand All @@ -422,21 +424,22 @@ export const Link = React.forwardRef<HTMLAnchorElement, LinkProps>(
let absoluteHref;
let isExternal = false;

if (
isBrowser &&
typeof to === "string" &&
/^(?:[a-z][a-z0-9+.-]*:|\/\/)/i.test(to)
) {
if (typeof to === "string" && ABSOLUTE_URL_REGEX.test(to)) {
// Render the absolute href server- and client-side
absoluteHref = to;
let currentUrl = new URL(window.location.href);
let targetUrl = to.startsWith("//")
? new URL(currentUrl.protocol + to)
: new URL(to);
if (targetUrl.origin === currentUrl.origin) {
// Strip the protocol/origin for same-origin absolute URLs
to = targetUrl.pathname + targetUrl.search + targetUrl.hash;
} else {
isExternal = true;

// Only check for external origins client-side
if (isBrowser) {
let currentUrl = new URL(window.location.href);
let targetUrl = to.startsWith("//")
? new URL(currentUrl.protocol + to)
: new URL(to);
if (targetUrl.origin === currentUrl.origin) {
// Strip the protocol/origin for same-origin absolute URLs
to = targetUrl.pathname + targetUrl.search + targetUrl.hash;
} else {
isExternal = true;
}
}
}

Expand Down