-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsvelte.config.js
executable file
·266 lines (233 loc) · 6.64 KB
/
svelte.config.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
import adapter from '@sveltejs/adapter-cloudflare';
import adapterStatic from '@sveltejs/adapter-static';
import {vitePreprocess} from '@sveltejs/kit/vite';
import pageKeys from './src/lib/data/pageKeys.js';
import {fetch} from 'undici';
import path from 'node:path';
import dotenv from 'dotenv';
// Mimic vites loading order using the dotenv default overwrite = false
// This means preexisting env vars have highest priority followed by
// env specific vars general env vars
// .local versions of the variables always overwrite their non-local
// counterpart
dotenv.config({
path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV}.local`),
});
dotenv.config({
path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV}`),
});
dotenv.config({path: path.resolve(process.cwd(), '.env.local')});
dotenv.config({path: path.resolve(process.cwd(), '.env')});
const mainRoutes = {
de: pageKeys['de'],
en: pageKeys['en'],
};
const URL = `${process.env.PUBLIC_API_URL}/graphql`;
function getAllowedStatus() {
const allowedStatus = ['published', 'published_anon'];
if (process.env.PUBLIC_PREVIEW === 'TRUE') {
allowedStatus.push('preview');
allowedStatus.push('preview_anon');
}
return allowedStatus;
}
if (
process.env.PUBLIC_ADAPTER === 'STATIC' &&
process.env.PUBLIC_PRERENDER !== 'ALL'
) {
throw Error('Env var ADAPTER=STATIC only allowed for PRERENDER=ALL');
}
function canBePrerendered(url) {
return (
url[0] === '/' &&
url !== '/volunteering/become-member' &&
url !== '/mitmachen/mitglied-werden'
);
}
const queries = {
blogs: `
query BlogSlugs($status: [String] = ["published"]) {
Blog_Posts(limit: -1, sort: ["-publication_datetime"], filter: {status: { _in: $status }}) {
translations(filter:{slug:{_neq:null}}) {
languages_code {
code
}
slug
}
}
}
`,
lcs: `
query LcSlugs {
Local_Chapters {
slug: short_id
}
}
`,
events: `
query EventSlugs($status: [String] = ["published"]) {
Events (limit: -1, filter : {status: {_in: $status}}){
slug
}
}
`,
projects: `
query ProjectSlugs($status: [String] = ["published"]) {
Projects (limit: -1, filter : {status: {_in: $status}}) {
slug: project_id
}
}
`,
jobs: `
query Jobs($status: [String] = ["published"]) {
Jobs(limit: -1, filter: { status: { _in: $status } }) {
slug
}
}
`,
};
function addBlogRoutesWithLanguageFallback(routes, translations) {
for (const t of translations) {
routes.push(`/blog/${t.slug}`);
routes.push(`/en/blog/${t.slug}`);
}
}
async function queryCmsGraphQl(query, vars) {
const payload = {query: query};
if (typeof vars !== 'undefined') {
payload['variables'] = vars;
}
const DIRECTUS_TOKEN = process.env.DIRECTUS_TOKEN;
const headers = {'Content-Type': 'application/json'};
if (
DIRECTUS_TOKEN !== undefined &&
DIRECTUS_TOKEN !== '' &&
DIRECTUS_TOKEN !== null
) {
headers.Authorization = `Bearer ${DIRECTUS_TOKEN}`;
}
const response = await fetch(URL, {
method: 'post',
body: JSON.stringify(payload),
headers: headers,
});
if (!response.ok) {
throw new Error(
`unexpected cms response ${response.statusText} for query ${
query.split(/\r?\n/)[0]
}`,
);
}
const data = await response.json();
if ('errors' in data) {
throw new Error(`Cms errors ${data.errors}`);
}
return data;
}
async function addBlogRoutes(routes) {
const postsResult = await queryCmsGraphQl(queries['blogs'], {
status: getAllowedStatus(),
});
for (const post of postsResult['data']['Blog_Posts']) {
addBlogRoutesWithLanguageFallback(routes, post['translations']);
}
}
async function addLcRoutes(routes) {
const germanResults = await queryCmsGraphQl(queries['lcs']);
for (const lc of germanResults['data']['Local_Chapters']) {
routes.push(`/mitmachen/correlaidx/${lc.slug.toLowerCase()}`);
routes.push(`/mitmachen/correlaidx/${lc.slug.toLowerCase()}/calendar.ics`);
}
const englishResults = await queryCmsGraphQl(queries['lcs']);
for (const lc of englishResults['data']['Local_Chapters']) {
routes.push(`/en/volunteering/correlaidx/${lc.slug.toLowerCase()}`);
routes.push(
`/en/volunteering/correlaidx/${lc.slug.toLowerCase()}/calendar.ics`,
);
}
}
async function addProjectRoutes(routes) {
const results = await queryCmsGraphQl(queries['projects'], {
status: getAllowedStatus(),
});
for (const project of results['data']['Projects']) {
routes.push(`/daten-nutzen/projektdatenbank/${project.slug}`);
routes.push(`/en/using-data/project-database/${project.slug}`);
}
}
async function addEventRoutes(routes) {
const results = await queryCmsGraphQl(queries['events'], {
status: getAllowedStatus(),
});
for (const event of results['data']['Events']) {
routes.push(`/veranstaltungen/${event.slug}`);
routes.push(`/en/events/${event.slug}`);
}
}
async function addJobRoutes(routes) {
const results = await queryCmsGraphQl(queries['jobs'], {
status: getAllowedStatus(),
});
for (const job of results['data']['Jobs']) {
routes.push(`/jobs/${job.slug}`);
routes.push(`/en/jobs/${job.slug}`);
}
}
const prerenderRoutes = [];
if (process.env.PUBLIC_PRERENDER === 'ALL') {
for (const routeName in mainRoutes['de']) {
if (
typeof routeName === 'string' &&
canBePrerendered(mainRoutes.de[routeName].url)
) {
prerenderRoutes.push(mainRoutes.de[routeName].url);
}
}
for (const routeName in mainRoutes['en']) {
if (
typeof routeName === 'string' &&
canBePrerendered(mainRoutes.en[routeName].url)
) {
prerenderRoutes.push(mainRoutes.en[routeName].url);
}
}
await addBlogRoutes(prerenderRoutes);
await addLcRoutes(prerenderRoutes);
await addProjectRoutes(prerenderRoutes);
await addEventRoutes(prerenderRoutes);
await addJobRoutes(prerenderRoutes);
prerenderRoutes.push('/404/');
prerenderRoutes.push('/en/404/');
} else {
prerenderRoutes.push('*');
}
let configuredAdapter;
if (process.env.PUBLIC_ADAPTER === 'STATIC') {
const staticBuildDir = process.env.BUILD_DIR || '.svelte-kit/cloudflare';
configuredAdapter = adapterStatic({
pages: staticBuildDir,
assets: staticBuildDir,
fallback: null,
precompress: false,
strict: false,
});
} else {
configuredAdapter = adapter({
routes: {
include: ['/*'],
exclude: ['<all>'],
},
});
}
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: configuredAdapter,
prerender: {entries: prerenderRoutes},
serviceWorker: {
register: false,
},
},
preprocess: vitePreprocess(),
};
export default config;