-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path[[...catchall]].tsx
73 lines (61 loc) · 2.23 KB
/
[[...catchall]].tsx
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
import { GetStaticPaths, GetStaticProps } from "next";
// Once you setup the splits in your studio project, you can import the following
// component to enable variations for the page.
import PlasmicSplitsProvider from "../../components/plasmic/codegen_custom_targettng/PlasmicSplitsProvider";
import AbTest from "../_splits/abtest";
import {
generateAllPaths,
rewriteWithoutTraits,
} from "@plasmicapp/loader-edge";
function SplitsABTest(props: { traits?: Record<string, string> }) {
const { traits } = props;
return (
<PlasmicSplitsProvider traits={traits}>
<AbTest plasmicSeed={traits?.["plasmic_seed"] ?? "unset"} />
</PlasmicSplitsProvider>
);
}
export const getStaticProps: GetStaticProps = async (context) => {
const { catchall } = context.params ?? {};
const rawPlasmicPath =
typeof catchall === "string"
? catchall
: Array.isArray(catchall)
? `/${catchall.join("/")}`
: "/";
const { traits } = rewriteWithoutTraits(rawPlasmicPath);
return {
props: { traits },
// For scheduled content to work, you must use revalidate, as
// the "current time" is not part of the page cache key, and is
// actually the time when this is run. So we rely on revalidate to
// invalidate the cached page content and regenerate with a
// new timestamp.
revalidate: 60,
};
};
export const getStaticPaths: GetStaticPaths = async () => {
function* gen() {
// This is going to generate all the possible paths for the current page
// with the seed trait set. It's possible to generate all the possible
// paths including traits by using `generateAllPathsWithTraits` instead.
const allPaths = generateAllPaths("/");
for (const path of allPaths) {
yield {
params: {
catchall: path.substring(1).split("/"),
},
};
}
}
return {
// We set `fallback:"blocking"` to generate page variations lazily.
// Once a page for a specific set of traits has been generated, it
// will be cached and reused. We use paths to generate some possible
// paths to be generated already in the build time to avoid the
// first request to be slow.
paths: Array.from(gen()),
fallback: "blocking",
};
};
export default SplitsABTest;