| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- // Pre-render the app into static HTML.
- // run `npm run generate` and then `dist/static` can be served as a static site.
- import fs from 'node:fs';
- import path from 'node:path';
- import url from 'node:url';
- const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
- const toAbsolute = (p) => path.resolve(__dirname, p);
- const manifest = JSON.parse(fs.readFileSync(toAbsolute('dist/static/ssr-manifest.json'), 'utf-8'));
- const template = fs.readFileSync(toAbsolute('dist/static/index.html'), 'utf-8');
- const { render } = await import('./dist/server/entry-server.mjs');
- const langs = JSON.parse(fs.readFileSync(`src/data/langs.json`), 'utf-8');
- const cards = JSON.parse(fs.readFileSync(`src/data/cards.json`), 'utf-8');
- const withoutLang = ['/', '/cards/grid', '/cards/list', '/cards/network', '/cards/quiz', '/langs', '/about'];
- for (const card of cards) {
- withoutLang.push('/card/' + card.id);
- }
- let paths = [];
- paths.push('/');
- for (const lang of langs) {
- paths = paths.concat(
- withoutLang.map((path) => {
- return '/' + lang.code + path;
- })
- );
- }
- paths = paths.concat(
- withoutLang.map((path) => {
- return '/' + 'en' + path;
- })
- );
- paths = paths.concat(
- withoutLang.map((path) => {
- return '/' + 'fr' + path;
- })
- );
- // // determine routes to pre-render from src/pages
- // const routesToPrerender = fs
- // .readdirSync(toAbsolute('src/pages'))
- // .map((file) => {
- // const name = file.replace(/\.vue$/, '').toLowerCase()
- // return name === 'home' ? `/` : `/${name}`
- // })
- (async () => {
- // pre-render each route...
- let k = 0;
- for (const url of paths) {
- console.log(`> prerendering ${url}`);
- const [appHtml, preloadLinks, state, metaTags] = await render(url, manifest);
- k += 1;
- console.log(`> Rendered ${k}/${paths.length} pages`);
- let html = template.replace(`<!--preload-links-->`, preloadLinks).replace(`'<pinia-store>'`, state).replace(`<!--app-html-->`, appHtml);
- for (const metaTag in metaTags) {
- html = html.replace(metaTag, metaTags[metaTag]);
- }
- let filePath = `dist/static${url.slice(-1) === '/' ? url + '/index' : url}.html`;
- filePath = filePath.replace('//', '/');
- fs.mkdirSync(filePath.split('/').slice(0, -1).join('/'), { recursive: true });
- fs.writeFileSync(toAbsolute(filePath), html);
- console.log('pre-rendered:', filePath);
- }
- // done, delete ssr manifest
- fs.unlinkSync(toAbsolute('dist/static/ssr-manifest.json'));
- })();
|