-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve prerender > check if is url need to be /folder/index
- Loading branch information
Willy Brauner
committed
Nov 17, 2022
1 parent
72503cd
commit 7f83490
Showing
5 changed files
with
102 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { expect, it } from "vitest" | ||
import { isRouteIndex } from "../helpers/isRouteIndex" | ||
|
||
it("should detect if URL is a route index", () => { | ||
// prettier-ignore | ||
const urls = [ | ||
"/", | ||
"/xp", | ||
"/xp-1", | ||
"/xp-2", | ||
"/x/p-2", | ||
"/experience", | ||
"/thanks/test", | ||
"/thanks", | ||
"/thanks/form", | ||
"/thanks/form/test", | ||
|
||
"/en", | ||
"/en/experience", | ||
"/en/thanks/test", | ||
"/en/thanks", | ||
"/en/thanks/form", | ||
"/en/thanks/form/test", | ||
] | ||
|
||
// url "/" always need to return true | ||
expect(isRouteIndex("/", urls)).toBe(true) | ||
|
||
// multi urls start with /xp but is not index | ||
// because no subRoute /xp /xp-1 /xp-2... | ||
expect(isRouteIndex("/xp", urls)).toBe(false) | ||
|
||
// this url doesn't exist, return false | ||
expect(isRouteIndex("/x", urls)).toBe(false) | ||
|
||
// ... | ||
expect(isRouteIndex("/thanks", urls)).toBe(true) | ||
expect(isRouteIndex("/thanks/test", urls)).toBe(false) | ||
expect(isRouteIndex("/thanks/form", urls)).toBe(true) | ||
expect(isRouteIndex("/thanks/form/test", urls)).toBe(false) | ||
|
||
// same with another language level | ||
expect(isRouteIndex("/en", urls)).toBe(true) | ||
expect(isRouteIndex("/en/xp", urls)).toBe(false) | ||
expect(isRouteIndex("/en/experience", urls)).toBe(false) | ||
expect(isRouteIndex("/en/thanks", urls)).toBe(true) | ||
expect(isRouteIndex("/en/thanks/test", urls)).toBe(false) | ||
expect(isRouteIndex("/en/thanks/form", urls)).toBe(true) | ||
expect(isRouteIndex("/en/thanks/form/test", urls)).toBe(false) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* Detect If current URL is a route index | ||
* If true, we want to generate /foo/index.html instead of "/foo.html" | ||
* | ||
* @param url Url to test | ||
* @param urls List of available URLs | ||
* @param log | ||
*/ | ||
export const isRouteIndex = (url, urls, log = false): boolean => { | ||
if (!urls.includes(url)) { | ||
console.warn(`isRouteIndex > ${url} isn't in the list, return false.`) | ||
return false | ||
} | ||
|
||
log && console.log("url", url) | ||
// if URL is "/" we want to generate /index.html | ||
if (url === "/") return true | ||
|
||
// get every URL of the list witch starting with same base | ||
const group = urls.filter((e) => e.startsWith(url)) | ||
log && console.log("group", group) | ||
|
||
// check if on of others in group is subRoute and not only same level route | ||
// witch starting with the same string | ||
// ex: ["/foo", "/foo-bar"] are on the same level, | ||
// ex: ["/foo", "/foo/bar"] are two different level route | ||
const subRouteExist = group.some((e) => e.slice(url.length).includes("/")) | ||
log && console.log("subRouteExist", subRouteExist) | ||
|
||
// if group is [ '/thanks/form', '/thanks' ] | ||
// we want the base route: "/thanks" | ||
const baseRoute = group?.sort((a, b) => a.length - b.length)?.[0] | ||
log && console.log("baseRoute", baseRoute) | ||
|
||
return ( | ||
// if group as more than 1 URL, this is a sub router case (or root case "/") | ||
// & if baseRoute equal to param URL, param URL should be the index page | ||
group.length > 1 && baseRoute === url && subRouteExist | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters