-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pre-render.js
167 lines (145 loc) · 6.1 KB
/
pre-render.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const puppeteer = require('puppeteer');
const fs = require('fs/promises');
const path = require('path');
const description = "ČSA je recesistická politická strana, v tento moment chystající se vzniknout a poté začít nabírat členy. Více než strana je ČSA internetová mikrokomunita, kde každý člověk náhodně z internetu může přispět svým dílem.";
const routes = [
{
path: '/',
title: 'Česká Strana Asociálů',
canonical: 'https://www.ceskastranaasocialu.cz/'
},
{
path: '/clenove',
title: 'Členové · ČSA',
canonical: 'https://www.ceskastranaasocialu.cz/clenove'
},
{
path: '/kontakty',
title: 'Kontakty · ČSA',
canonical: 'https://www.ceskastranaasocialu.cz/kontakty'
},
{
path: '/historie',
title: 'Historie · ČSA',
canonical: 'https://www.ceskastranaasocialu.cz/historie'
},
{
path: '/pomoc',
title: 'Chci pomoci · ČSA',
canonical: 'https://www.ceskastranaasocialu.cz/pomoc',
image: '/assets/bannerDEJTENÁMVAŠEVŠECHNYPRACHY.png'
},
{
path: '/source',
title: 'Zdroj · ČSA',
canonical: 'https://www.ceskastranaasocialu.cz/source'
}
];
async function prerender() {
const browser = await puppeteer.launch();
const page = await browser.newPage();
for (const route of routes) {
const fileName = route.path === '/' ? 'index.html' : `${route.path.slice(1)}/index.html`;
const filePath = path.join(__dirname, 'build', fileName);
// Create directory if it doesn't exist
const dir = path.dirname(filePath);
await fs.mkdir(dir, { recursive: true });
// Load the page
const fullUrl = `file://${path.join(__dirname, 'build', 'index.html')}`;
await page.goto(fullUrl);
await page.waitForSelector('#root');
// Inject meta tags
await page.evaluate(({ title, description, canonical, image }) => {
// Set title
document.title = title;
// Set description
let metaDescription = document.querySelector('meta[name="description"]');
if (!metaDescription) {
metaDescription = document.createElement('meta');
metaDescription.name = 'description';
document.head.appendChild(metaDescription);
}
metaDescription.content = description;
// Set Open Graph meta tags
const ogTags = {
'og:title': title,
'og:description': description,
'og:image': image || '/assets/banner.png',
'og:url': canonical,
'og:type': 'website'
};
for (const [property, content] of Object.entries(ogTags)) {
let ogTag = document.querySelector(`meta[property="${property}"]`);
if (!ogTag) {
ogTag = document.createElement('meta');
ogTag.setAttribute('property', property);
document.head.appendChild(ogTag);
}
ogTag.content = content;
}
// Set Twitter meta tags
const TwitterTags = {
'twitter:title': title,
'twitter:description': description,
'twitter:image': image || '/assets/banner.png',
'twitter:site': '@CASocialu',
'twitter:card': 'summary_large_image'
};
for (const [property, content] of Object.entries(TwitterTags)) {
let twitterTag = document.querySelector(`meta[name="${property}"]`);
if (!twitterTag) {
twitterTag = document.createElement('meta');
twitterTag.setAttribute('name', property);
document.head.appendChild(twitterTag);
}
twitterTag.content = content;
}
// Set theme-color meta tag
let themeColorMeta = document.querySelector('meta[name="theme-color"]');
if (!themeColorMeta) {
themeColorMeta = document.createElement('meta');
themeColorMeta.name = 'theme-color';
document.head.appendChild(themeColorMeta);
}
themeColorMeta.content = '#009074';
// Set canonical URL
let canonicalLink = document.querySelector('link[rel="canonical"]');
if (!canonicalLink) {
canonicalLink = document.createElement('link');
canonicalLink.rel = 'canonical';
document.head.appendChild(canonicalLink);
}
canonicalLink.href = canonical;
// Set robots meta tag
let robots = document.querySelector('meta[name="robots"]');
if (!robots) {
robots = document.createElement('meta');
robots.name = 'robots';
document.head.appendChild(robots);
}
robots.content = 'index, follow';
// Apply stylesheet link
let stylesheetLink = document.querySelector('link[href="/index.css"]');
if (!stylesheetLink) {
stylesheetLink = document.createElement('link');
stylesheetLink.rel = 'stylesheet';
stylesheetLink.href = '/index.css';
document.head.appendChild(stylesheetLink);
}
document.documentElement.setAttribute('data-location', new URL(canonical).pathname.replace(/(?<!^)\/$/, ''));
}, { ...route, description });
const html = await page.content();
await fs.writeFile(filePath, html);
}
// Generate sitemap.xml
const sitemapContent = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${routes.map(route => ` <url>
<loc>${route.canonical}</loc>
<changefreq>weekly</changefreq>
</url>`).join('\n')}
</urlset>`;
await fs.writeFile(path.join(__dirname, 'build', 'sitemap.xml'), sitemapContent);
await browser.close();
}
prerender().catch(console.error);