mon-entreprise/site/build/prerender.ts

113 lines
3.2 KiB
TypeScript
Raw Normal View History

import { readFileSync, statSync, writeFileSync } from 'node:fs'
import { resolve } from 'node:path'
import { argv } from 'node:process'
2023-06-15 13:57:48 +00:00
import Tinypool from 'tinypool'
2022-11-03 16:32:04 +00:00
2023-09-12 07:37:17 +00:00
import { absoluteSitePaths } from '../source/sitePaths.js'
2023-09-25 08:03:58 +00:00
const filename = new URL('./prerender-worker.ts', import.meta.url).href
const pool = new Tinypool({
filename,
execArgv: ['--loader', 'ts-node/esm'],
2023-09-25 08:03:58 +00:00
idleTimeout: 2000,
})
2022-07-28 18:04:44 +00:00
const sitePathFr = absoluteSitePaths.fr
const sitePathEn = absoluteSitePaths.en
export const pagesToPrerender: {
'mon-entreprise': string[]
infrance: string[]
} = {
'mon-entreprise': [
'/iframes/pamc',
'/iframes/simulateur-embauche',
'/iframes/simulateur-independant',
'/iframes/simulateur-autoentrepreneur',
'/iframes/simulateur-assimilesalarie',
'/iframes/simulateur-eurl',
'/iframes/profession-liberale',
'/iframes/auxiliaire-medical',
'/iframes/medecin',
'/iframes/choix-statut-juridique',
sitePathFr.assistants['choix-du-statut'].index,
sitePathFr.assistants['recherche-code-ape'],
sitePathFr.index,
2023-06-22 12:26:14 +00:00
sitePathFr.simulateursEtAssistants,
sitePathFr.simulateurs.index,
sitePathFr.simulateurs.comparaison,
sitePathFr.simulateurs.dividendes,
sitePathFr.simulateurs.eurl,
sitePathFr.simulateurs.indépendant,
sitePathFr.simulateurs.is,
sitePathFr.simulateurs.salarié,
sitePathFr.simulateurs.sasu,
sitePathFr.simulateurs['artiste-auteur'],
sitePathFr.simulateurs['auto-entrepreneur'],
sitePathFr.simulateurs['chômage-partiel'],
sitePathFr.simulateurs['coût-création-entreprise'],
sitePathFr.simulateurs['entreprise-individuelle'],
sitePathFr.simulateurs['profession-libérale'].avocat,
sitePathFr.simulateurs['profession-libérale']['chirurgien-dentiste'],
sitePathFr.simulateurs['profession-libérale'].index,
].map((val) => encodeURI(val)),
infrance: [
sitePathEn.index,
sitePathEn.simulateurs.salarié,
'/iframes/simulateur-embauche',
].map((val) => encodeURI(val)),
}
const dev = argv.findIndex((val) => val === '--dev') > -1
const redirects = await Promise.all(
Object.entries(pagesToPrerender).flatMap(([site, urls]) =>
2023-09-25 08:03:58 +00:00
urls.map(async (url) => {
const path = await (pool.run({
site,
url,
lang: site === 'mon-entreprise' ? 'fr' : 'en',
}) as Promise<string>)
// eslint-disable-next-line no-console
console.log(`preredering ${url} done, adding redirect`)
return `
[[redirects]]
from = ":SITE_${site === 'mon-entreprise' ? 'FR' : 'EN'}${
2023-08-01 08:39:13 +00:00
dev ? decodeURI(url) : url
}"
to = "/${path}"
status = 200
${dev ? ' force = true\n' : ''}`
2023-09-25 08:03:58 +00:00
})
)
)
// 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(''))
)
2022-06-27 08:20:36 +00:00
// eslint-disable-next-line no-console
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)
}
}