diff --git a/src/core/url.js b/src/core/url.js index ec7955fad..a528f39f3 100644 --- a/src/core/url.js +++ b/src/core/url.js @@ -25,7 +25,7 @@ export function getExtension(url) { } export function isPrefixedBy(baseURL, url) { - const prefix = getPrefix(url) + const prefix = getPrefix(url) || '/' return baseURL.href === expandURL(prefix).href || baseURL.href.startsWith(prefix) } @@ -55,9 +55,5 @@ function getLastPathComponent(url) { } function getPrefix(url) { - return addTrailingSlash(url.origin + url.pathname) -} - -function addTrailingSlash(value) { - return value.endsWith("/") ? value : value + "/" + url.origin + url.pathname } diff --git a/src/tests/fixtures/root/index.html b/src/tests/fixtures/root/index.html new file mode 100644 index 000000000..3563f3f65 --- /dev/null +++ b/src/tests/fixtures/root/index.html @@ -0,0 +1,18 @@ + + + + + Turbo + + + + + +
+

Root

+

Link to page inside root

+

Link to page outside root

+
+
+ + diff --git a/src/tests/fixtures/root/page.html b/src/tests/fixtures/root/page.html new file mode 100644 index 000000000..9fa53bf71 --- /dev/null +++ b/src/tests/fixtures/root/page.html @@ -0,0 +1,17 @@ + + + + + Turbo + + + + + +
+

Root

+

Link to root

+
+
+ + diff --git a/src/tests/functional/root_test.js b/src/tests/functional/root_test.js new file mode 100644 index 000000000..88c320e71 --- /dev/null +++ b/src/tests/functional/root_test.js @@ -0,0 +1,27 @@ +import { test } from "@playwright/test" +import { assert } from "chai" +import { nextBody, pathname, visitAction } from "../helpers/page" + +test("test visiting a location inside the root", async ({ page }) => { + page.goto("/src/tests/fixtures/root/index.html") + page.click("#link-page-inside") + await nextBody(page) + assert.equal(pathname(page.url()), "/src/tests/fixtures/root/page.html") + assert.notEqual(await visitAction(page), "load") +}) + +test("test visiting the root itself", async ({ page }) => { + page.goto("/src/tests/fixtures/root/page.html") + page.click("#link-root") + await nextBody(page) + assert.equal(pathname(page.url()), "/src/tests/fixtures/root/") + assert.notEqual(await visitAction(page), "load") +}) + +test("test visiting a location outside the root", async ({ page }) => { + page.goto("/src/tests/fixtures/root/index.html") + page.click("#link-page-outside") + await nextBody(page) + assert.equal(pathname(page.url()), "/src/tests/fixtures/one.html") + assert.equal(await visitAction(page), "load") +})