-
Notifications
You must be signed in to change notification settings - Fork 156
Expand file tree
/
Copy path[...identifier].astro
More file actions
44 lines (36 loc) · 1.22 KB
/
[...identifier].astro
File metadata and controls
44 lines (36 loc) · 1.22 KB
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
---
import {
uriToTemplate,
createDefaultClient,
setGraphQLClient,
} from '@faustjs/astro';
import { getAuthString } from '@/utils/getAuthString.js';
// Determine if we are in preview mode based on the URL parameter and the secret
const isPreview =
Astro.url.searchParams.get('preview') === 'true' &&
import.meta.env.WP_PREVIEW_SECRET === Astro.url.searchParams.get('secret');
const headers = isPreview ? { Authorization: getAuthString() } : undefined;
// Set up GraphQL client using environment variable
const client = createDefaultClient(import.meta.env.WORDPRESS_URL, headers);
setGraphQLClient(client);
const { identifier = '/' } = Astro.params;
// Fetch template data based on the URI and preview mode
const templateData = await uriToTemplate(
isPreview
? {
id: identifier,
asPreview: true,
}
: { uri: identifier },
);
// Make data available to other templates and components
Astro.locals.templateData = templateData;
Astro.locals.isPreview = isPreview || false;
Astro.locals.client = client;
// If a template was found, rewrite to that template's path
if (templateData.template) {
return Astro.rewrite(templateData.template.path);
}
// If no template was found, redirect to 404
return Astro.rewrite('/404');
---