@vigilkids/cms-nuxt 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/module.json +1 -1
- package/dist/module.mjs +4 -0
- package/dist/runtime/composables/useCmsPreview.js +19 -6
- package/dist/runtime/server/api/_revalidate.post.js +5 -3
- package/dist/runtime/server/api/preview.get.js +1 -1
- package/dist/runtime/server/middleware/preview-context.d.ts +0 -0
- package/dist/runtime/server/middleware/preview-context.js +10 -0
- package/package.json +1 -1
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -49,6 +49,10 @@ const module = defineNuxtModule({
|
|
|
49
49
|
});
|
|
50
50
|
}
|
|
51
51
|
if (options.preview?.enabled !== false) {
|
|
52
|
+
addServerHandler({
|
|
53
|
+
middleware: true,
|
|
54
|
+
handler: resolve("./runtime/server/middleware/preview-context")
|
|
55
|
+
});
|
|
52
56
|
addServerHandler({
|
|
53
57
|
route: "/api/preview",
|
|
54
58
|
method: "get",
|
|
@@ -1,10 +1,23 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { computed } from "vue";
|
|
1
|
+
import { useCookie, useRequestEvent, useRoute, useRuntimeConfig } from "#imports";
|
|
2
|
+
import { computed, ref } from "vue";
|
|
3
|
+
export function useCmsPreviewToken() {
|
|
4
|
+
const config = useRuntimeConfig();
|
|
5
|
+
const cookieName = config.public.cms.preview?.cookieName ?? "__preview_token";
|
|
6
|
+
if (import.meta.server) {
|
|
7
|
+
const event = useRequestEvent();
|
|
8
|
+
const token = event?.context?.__cms_preview_token ?? null;
|
|
9
|
+
return ref(token);
|
|
10
|
+
}
|
|
11
|
+
const cookie = useCookie(cookieName);
|
|
12
|
+
return computed(
|
|
13
|
+
() => cookie.value ? decodeURIComponent(cookie.value) : null
|
|
14
|
+
);
|
|
15
|
+
}
|
|
3
16
|
export function useCmsPreview() {
|
|
4
|
-
const
|
|
5
|
-
const
|
|
17
|
+
const token = useCmsPreviewToken();
|
|
18
|
+
const route = useRoute();
|
|
19
|
+
const isPreview = computed(() => !!token.value || route.query._preview === "1");
|
|
6
20
|
function exitPreview() {
|
|
7
|
-
previewToken.value = null;
|
|
8
21
|
if (import.meta.client) {
|
|
9
22
|
const config = useRuntimeConfig();
|
|
10
23
|
const cookieName = config.public.cms.preview?.cookieName ?? "__preview_token";
|
|
@@ -12,5 +25,5 @@ export function useCmsPreview() {
|
|
|
12
25
|
window.location.reload();
|
|
13
26
|
}
|
|
14
27
|
}
|
|
15
|
-
return { isPreview, previewToken, exitPreview };
|
|
28
|
+
return { isPreview, previewToken: token, exitPreview };
|
|
16
29
|
}
|
|
@@ -29,11 +29,13 @@ export default defineEventHandler(async (event) => {
|
|
|
29
29
|
paths.push(`${localePrefix}/blog/${payload.slug}`);
|
|
30
30
|
paths.push(`${localePrefix}/blog`);
|
|
31
31
|
const storage = useStorage("cache:nitro:routes");
|
|
32
|
+
const allKeys = await storage.getKeys("_:");
|
|
32
33
|
let cleared = 0;
|
|
33
34
|
for (const path of paths) {
|
|
34
|
-
const
|
|
35
|
-
|
|
36
|
-
|
|
35
|
+
const escapedPrefix = path.replace(/\W/g, "").slice(0, 16);
|
|
36
|
+
const matchingKeys = allKeys.filter((k) => k.startsWith(`_:${escapedPrefix}.`));
|
|
37
|
+
for (const key of matchingKeys) {
|
|
38
|
+
await storage.removeItem(key);
|
|
37
39
|
cleared++;
|
|
38
40
|
}
|
|
39
41
|
}
|
|
@@ -27,5 +27,5 @@ export default defineEventHandler(async (event) => {
|
|
|
27
27
|
}
|
|
28
28
|
const articlePath = cmsConfig.preview?.articlePath ?? "/blog/{slug}";
|
|
29
29
|
const path = articlePath.replace("{slug}", slug);
|
|
30
|
-
return sendRedirect(event, `${prefix}${path}`, 302);
|
|
30
|
+
return sendRedirect(event, `${prefix}${path}?_preview=1`, 302);
|
|
31
31
|
});
|
|
File without changes
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { useRuntimeConfig } from "#imports";
|
|
2
|
+
import { defineEventHandler, getCookie } from "h3";
|
|
3
|
+
export default defineEventHandler((event) => {
|
|
4
|
+
const config = useRuntimeConfig();
|
|
5
|
+
const cookieName = config.public.cms.preview?.cookieName ?? "__preview_token";
|
|
6
|
+
const raw = getCookie(event, cookieName);
|
|
7
|
+
if (raw) {
|
|
8
|
+
event.context.__cms_preview_token = decodeURIComponent(raw);
|
|
9
|
+
}
|
|
10
|
+
});
|