Skip to content

Commit

Permalink
Improve prerender > check if is url need to be /folder/index
Browse files Browse the repository at this point in the history
  • Loading branch information
Willy Brauner committed Nov 17, 2022
1 parent 72503cd commit 7f83490
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 12 deletions.
50 changes: 50 additions & 0 deletions prerender/__tests__/isRouterIndex.test.ts
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)
})
40 changes: 40 additions & 0 deletions prerender/helpers/isRouteIndex.ts
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
)
}
21 changes: 10 additions & 11 deletions prerender/prerender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ import path from "path"
import { render } from "~/index-server"
import config from "../config/config.js"
import palette from "../config/helpers/palette.js"
const { log } = console
import { isRouteIndex } from "./helpers/isRouteIndex"

export const prerender = async (urls: string[], outDirStatic = config.outDirStatic) => {
log("URLs to generate", urls)

console.log("URLs to generate", urls)
const indexTemplateSrc = `${outDirStatic}/index-template.html`

// copy index as template to avoid the override with the generated static index.html bellow
Expand All @@ -21,15 +20,15 @@ export const prerender = async (urls: string[], outDirStatic = config.outDirStat
for (const url of urls) {
let preparedUrl = url.startsWith("/") ? url : `/${url}`

// get react app HTML render to string
try {
const { meta, renderToString, ssrStaticProps, globalData, languages } =
await render(preparedUrl, true)
if (preparedUrl === "/") preparedUrl = "/index"

if (languages && languages.some((e) => `/${e.key}` === preparedUrl)) {
preparedUrl = `${preparedUrl}/index`
}
// Request information from render method
const { renderToString, ssrStaticProps, globalData, meta } = await render(
preparedUrl,
true
)

// Case url is index of root or of index of a group
if (isRouteIndex(preparedUrl, urls)) preparedUrl = `${preparedUrl}/index`

// include it in the template
const template = layout
Expand Down
1 change: 1 addition & 0 deletions prerender/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const fetchAvailableUrls = async (): Promise<string[]> => {
return new Promise(resolve => {
resolve([
"/",
"/work",
"/work/first-work",
"/work/second-work",
"/404"
Expand Down
2 changes: 1 addition & 1 deletion src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const routes: TRoute[] = [
},
},
{
path: "/work/:slug",
path: "/work/:slug?",
name: EPages.WORK,
component: WorkPage,
getStaticProps: async (props) => {
Expand Down

0 comments on commit 7f83490

Please sign in to comment.