Generate netlify.toml prerender redirection

pull/2175/head
Jérémy Rialland 2022-06-20 16:37:30 +02:00 committed by Jérémy Rialland
parent 61477f9a9c
commit 51d9dfd02e
9 changed files with 68 additions and 105 deletions

View File

@ -62,7 +62,7 @@ jobs:
- name: Build app SSR
run: |
yarn workspace site build:ssr
yarn workspace site build:prerender
yarn workspace site build:prerender --netlify-toml-path ./netlify.toml
- name: Replace site placeholders in netlify.toml redirection file
run: |
sed -i "s|:API_URL|$API_BASE_URL|g" site/netlify.toml;

1
site/.gitignore vendored
View File

@ -5,4 +5,5 @@ cypress/videos
cypress/screenshots
cypress/downloads
.deps.json
netlify.dev.toml
source/public/sitemap.*.txt

View File

@ -1,17 +1,20 @@
import { fr } from '../../../support/utils'
// @ts-ignore
import prerenderPaths from '../../../prerender-paths.json'
describe('Test prerender', function () {
const paths = (
prerenderPaths as { 'mon-entreprise': string[]; infrance: string[] }
)[fr ? 'mon-entreprise' : 'infrance']
const tests = {
'mon-entreprise': [
'/simulateurs/salaire-brut-net',
'/simulateurs/indépendant',
'/iframes/simulateur-embauche',
],
infrance: ['', '/calculators/salary', '/iframes/simulateur-embauche'],
}
paths.forEach((path) => {
tests[fr ? 'mon-entreprise' : 'infrance'].forEach((path) => {
it(`should show the pre-render of ${fr ? 'fr' : 'en'} ${
path || '/'
}`, function () {
cy.visit(encodeURI(path || '/'), { script: false })
cy.visit(path || '/', { script: false })
.get('#loading', { timeout: 200 })
.should('not.exist')
})

View File

@ -1 +0,0 @@
{"mon-entreprise":["","/créer","/gérer","/simulateurs","/simulateurs/salaire-brut-net","/simulateurs/chômage-partiel","/simulateurs/auto-entrepreneur","/simulateurs/indépendant","/simulateurs/sasu","/simulateurs/artiste-auteur","/iframes/simulateur-embauche","/iframes/pamc"],"infrance":["","/calculators/salary","/iframes/simulateur-embauche"]}

View File

@ -93,7 +93,11 @@ Content-Security-Policy = "default-src 'self' mon-entreprise.fr; style-src 'self
############
# Redirects for single page app config & prerendering purpose
# Prerender will be added by script build:prerender
#[prerender]#
############
# Redirects for single page app config
# Mon-entreprise.fr
@ -107,66 +111,6 @@ Content-Security-Policy = "default-src 'self' mon-entreprise.fr; style-src 'self
to = "/sitemap.fr.txt"
status = 200
[[redirects]]
from = ":SITE_FR"
to = "/prerender/mon-entreprise/index.html"
status = 200
[[redirects]]
from = ":SITE_FR/cr%C3%A9er"
to = "/prerender/mon-entreprise/cr%C3%A9er/index.html"
status = 200
[[redirects]]
from = ":SITE_FR/g%C3%A9rer"
to = "/prerender/mon-entreprise/g%C3%A9rer/index.html"
status = 200
[[redirects]]
from = ":SITE_FR/simulateurs"
to = "/prerender/mon-entreprise/simulateurs/index.html"
status = 200
[[redirects]]
from = ":SITE_FR/simulateurs/ch%C3%B4mage-partiel"
to = "/prerender/mon-entreprise/simulateurs/ch%C3%B4mage-partiel/index.html"
status = 200
[[redirects]]
from = ":SITE_FR/simulateurs/salaire-brut-net"
to = "/prerender/mon-entreprise/simulateurs/salaire-brut-net/index.html"
status = 200
[[redirects]]
from = ":SITE_FR/simulateurs/auto-entrepreneur"
to = "/prerender/mon-entreprise/simulateurs/auto-entrepreneur/index.html"
status = 200
[[redirects]]
from = ":SITE_FR/simulateurs/ind%C3%A9pendant"
to = "/prerender/mon-entreprise/simulateurs/ind%C3%A9pendant/index.html"
status = 200
[[redirects]]
from = ":SITE_FR/simulateurs/sasu"
to = "/prerender/mon-entreprise/simulateurs/sasu/index.html"
status = 200
[[redirects]]
from = ":SITE_FR/simulateurs/artiste-auteur"
to = "/prerender/mon-entreprise/simulateurs/artiste-auteur/index.html"
status = 200
[[redirects]]
from = ":SITE_FR/iframes/simulateur-embauche"
to = "/prerender/mon-entreprise/iframes/simulateur-embauche/index.html"
status = 200
[[redirects]]
from = ":SITE_FR/iframes/pamc"
to = "/prerender/mon-entreprise/iframes/pamc/index.html"
status = 200
[[redirects]]
from = ":SITE_FR/dev/storybook"
to = "/dev/storybook/index.html"
@ -191,21 +135,6 @@ Content-Security-Policy = "default-src 'self' mon-entreprise.fr; style-src 'self
to = "/sitemap.en.txt"
status = 200
[[redirects]]
from = ":SITE_EN"
to = "/prerender/infrance/index.html"
status = 200
[[redirects]]
from = ":SITE_EN/calculators/salary"
to = "/prerender/infrance/calculators/salary/index.html"
status = 200
[[redirects]]
from = ":SITE_EN/iframes/simulateur-embauche"
to = "/prerender/infrance/iframes/simulateur-embauche/index.html"
status = 200
[[redirects]]
from = ":SITE_EN/dev/storybook"
to = "/dev/storybook/index.html"

View File

@ -26,10 +26,10 @@
"build:ssr": "NODE_OPTIONS='--max-old-space-size=4096'; vite build --ssr ./source/entry-server.tsx --outDir ./dist/server --emptyOutDir && echo '{\"module\": \"commonjs\"}' > dist/package.json",
"build:prerender": "ts-node-esm prerender.ts",
"build:iframe-script": "NODE_OPTIONS='--max-old-space-size=4096'; vite build --config vite-iframe-script.config.ts",
"build:preview": "VITE_FR_BASE_URL=http://localhost:8888; VITE_EN_BASE_URL=http://localhost:8889; yarn build && yarn build:ssr && yarn build:prerender",
"build:sitemap": "ts-node-esm scripts/build-sitemap.ts",
"preview:mon-entreprise": "sed 's|:SITE_EN|_|g' netlify.toml | sed 's|:SITE_FR||g' | sed 's|:API_URL|http://localhost:3004|g' > dist/netlify.toml && cd dist && npx netlify-cli dev -d ./ -p 8888",
"preview:infrance": " sed 's|:SITE_EN||g' netlify.toml | sed 's|:SITE_FR|_|g' | sed 's|:API_URL|http://localhost:3004|g' > dist/netlify.toml && cd dist && npx netlify-cli dev -d ./ -p 8889",
"build:preview": "VITE_FR_BASE_URL=http://localhost:8888; VITE_EN_BASE_URL=http://localhost:8889; yarn build && yarn build:ssr && cp ./netlify.toml ./netlify.dev.toml && yarn build:prerender --dev --netlify-toml-path ./netlify.dev.toml",
"preview:mon-entreprise": "sed 's|:SITE_EN|_|g' netlify.dev.toml | sed 's|:SITE_FR||g' | sed 's|:API_URL|http://localhost:3004|g' > dist/netlify.toml && cd dist && npx netlify-cli dev -d ./ -p 8888",
"preview:infrance": " sed 's|:SITE_EN||g' netlify.dev.toml | sed 's|:SITE_FR|_|g' | sed 's|:API_URL|http://localhost:3004|g' > dist/netlify.toml && cd dist && npx netlify-cli dev -d ./ -p 8889",
"typecheck:watch": "tsc --skipLibCheck --noEmit --watch",
"test": "vitest",
"test:dev-e2e:mon-entreprise": "cypress open --e2e",

View File

@ -35,4 +35,6 @@ export default async ({ site, url, lang }) => {
await fs.mkdir(dir, { recursive: true })
await fs.writeFile(path.join(dir, 'index.html'), page)
return path.relative(path.join(dirname, 'dist'), path.join(dir, 'index.html'))
}

View File

@ -1,4 +1,6 @@
import { writeFileSync } from 'node:fs'
import { readFileSync, statSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { argv } from 'node:process'
import Tinypool from 'tinypool'
import { constructLocalizedSitePath } from './source/sitePaths.js'
@ -33,21 +35,52 @@ export const pagesToPrerender: {
],
}
if (process.env.GENERATE_PRERENDER_PATHS_JSON) {
// This json file is used in e2e cypress test
writeFileSync(
'cypress/prerender-paths.json',
JSON.stringify(pagesToPrerender)
)
console.log('cypress/prerender-paths.json was generated!')
const dev = argv.findIndex((val) => val === '--dev') > -1
process.exit()
}
await Promise.all(
const redirects = await Promise.all(
Object.entries(pagesToPrerender).flatMap(([site, urls]) =>
urls.map((url) =>
pool.run({ site, url, lang: site === 'mon-entreprise' ? 'fr' : 'en' })
pool
.run({
site,
url,
lang: site === 'mon-entreprise' ? 'fr' : 'en',
})
.then((path: string) => {
return `
[[redirects]]
from = ":SITE_${site === 'mon-entreprise' ? 'FR' : 'EN'}${
dev ? url : encodeURI(url)
}"
to = "/${path}"
status = 200
${dev ? ' force = true\n' : ''}`
})
)
)
)
// Replace the #[prerender]# tag in netlify.toml if --netlify-toml-path is specified
const index = argv.findIndex((val) => val === '--netlify-toml-path')
if (index > -1 && argv[index + 1]) {
const netlifyTomlPath = resolve(argv[index + 1])
if (statSync(netlifyTomlPath).isFile()) {
const data = readFileSync(netlifyTomlPath, { encoding: 'utf8' })
if (/#\[prerender\]#/g.test(data)) {
writeFileSync(
netlifyTomlPath,
data.replace(/#\[prerender\]#/g, redirects.join(''))
)
console.log('Redirects added to ' + netlifyTomlPath)
} else {
throw new Error('tag #[prerender]# not found in ' + netlifyTomlPath)
}
} else {
throw new Error('this path is not a file' + netlifyTomlPath)
}
}

View File

@ -13,10 +13,6 @@ const results = await execOnFileChange({
],
run: 'yarn build:yaml-to-dts',
},
{
paths: ['./prerender.ts'],
run: 'GENERATE_PRERENDER_PATHS_JSON=true yarn build:prerender',
},
],
})