@justanarthur/payload-www 1.4.2 → 2.0.1

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.
@@ -1,7 +1,4 @@
1
-
2
-
3
1
  // src/render/pages/FooterPage.tsx
4
- import"server-only";
5
2
  import { jsx, jsxs } from "react/jsx-runtime";
6
3
  function FooterPage({ data }) {
7
4
  const nav = data.nav ?? [];
@@ -59,7 +56,6 @@ function FooterPage({ data }) {
59
56
  }
60
57
 
61
58
  // src/render/pages/HeaderPage.tsx
62
- import"server-only";
63
59
  import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
64
60
  function HeaderPage({ data }) {
65
61
  const nav = data.nav ?? [];
@@ -208,7 +204,7 @@ async function PostsPage({ doc, locale, ...props }) {
208
204
  }
209
205
 
210
206
  // src/render/pages/createCollectionPageExports.tsx
211
- import"server-only";
207
+ import * as React from "react";
212
208
 
213
209
  // src/render/metadata/slug.ts
214
210
  var SLUG_NESTED_DIVIDER = "_";
@@ -229,135 +225,112 @@ function slugToPath(slug) {
229
225
  }
230
226
 
231
227
  // src/render/metadata/query.ts
228
+ import { createCacheHelpers } from "@pro-laico/payload-revalidate/cache";
229
+ import { tagsFor } from "@pro-laico/payload-revalidate";
230
+ import { cacheLife } from "next/cache";
232
231
  import { getPayload } from "payload";
233
-
234
- // src/collections/hooks/createRevalidateCollectionGlobalHook.ts
235
- import { revalidateTag } from "next/cache";
236
- function createRevalidateCollectionGlobalHook() {
237
- const afterChangeDeleteHook = async ({ doc, req, ...args }) => {
238
- const previousDoc = "previousDoc" in args ? args.previousDoc : null, locale = req.locale;
239
- function revalidate(args2) {
240
- try {
241
- revalidateTag(createCollectionCacheKey({ ...args2, locale }), "max");
242
- } catch (error) {
243
- const msg = error instanceof Error ? error.message : String(error);
244
- if (msg.includes("static generation store missing"))
245
- return;
246
- console.error(error);
247
- }
248
- }
249
- if ("collection" in args) {
250
- [
251
- doc && doc._status !== "draft" && doc.slug,
252
- previousDoc && previousDoc._status === "published" && previousDoc.slug
253
- ].filter((slug) => slug !== undefined && slug !== false).map((slug) => revalidate({ collectionSlug: args.collection.slug, slug }));
254
- } else {
255
- revalidate({ globalSlug: args.global.slug });
256
- }
257
- return doc;
258
- };
259
- return { afterChange: afterChangeDeleteHook, afterDelete: afterChangeDeleteHook };
260
- }
261
- function createCollectionCacheKey(args) {
262
- return `${"globalSlug" in args ? args.globalSlug : args.collectionSlug + args.slug}_${args.locale}`;
232
+ var _helpers = null;
233
+ var _seedPromise = null;
234
+ function seedPayloadCache({ config }) {
235
+ if (_helpers || _seedPromise)
236
+ return;
237
+ _seedPromise = (async () => {
238
+ const payload = await getPayload({ config: await config });
239
+ _helpers = createCacheHelpers(payload);
240
+ })();
263
241
  }
264
-
265
- // src/render/metadata/query.ts
266
- async function withUnstableCache(keyParts, tags, fn) {
267
- const { unstable_cache } = await import("next/cache");
268
- return unstable_cache(fn, keyParts.map(String), { tags })();
242
+ async function requireCacheHelpers() {
243
+ if (_helpers)
244
+ return _helpers;
245
+ if (_seedPromise) {
246
+ await _seedPromise;
247
+ if (_helpers)
248
+ return _helpers;
249
+ }
250
+ throw new Error("[payload-www] seedPayloadCache({ config }) must be called before any query getter. " + "Call it from createCollectionPageExports / createRootLayoutExports factory, or in your root layout.");
269
251
  }
270
- async function queryDoc(args, { config: configPromise }) {
271
- return withUnstableCache(Object.values(args), [createCollectionCacheKey(args)], async () => {
272
- if ("globalSlug" in args)
273
- return queryGlobal({ ...args, config: configPromise });
274
- else
275
- return queryDocBySlug({ ...args, config: configPromise });
252
+ async function queryDocBySlug(args) {
253
+ "use cache";
254
+ cacheLife("weeks");
255
+ const { findDoc } = await requireCacheHelpers();
256
+ const slugField = args.slugField ?? "slug";
257
+ const result = await findDoc(args.collectionSlug, {
258
+ where: { [slugField]: { equals: args.slug } },
259
+ locale: args.locale,
260
+ draft: args.draft ?? false,
261
+ depth: args.depth
276
262
  });
263
+ return result ?? null;
277
264
  }
278
- async function queryDocBySlug({
279
- collectionSlug,
280
- slug,
281
- slugField = "slug",
282
- locale,
283
- draft = false,
284
- config
285
- }) {
286
- return withUnstableCache([collectionSlug, slug, locale, draft], [createCollectionCacheKey({ collectionSlug, slug, locale })], async () => {
287
- const payload = await getPayload({ config });
288
- const result = await payload.find({
289
- collection: collectionSlug,
290
- draft,
291
- limit: 1,
292
- pagination: false,
293
- overrideAccess: draft,
294
- where: { [slugField]: { equals: slug } },
295
- locale
265
+ async function queryGlobal(args) {
266
+ "use cache";
267
+ cacheLife("weeks");
268
+ const { findGlobal } = await requireCacheHelpers();
269
+ try {
270
+ const result = await findGlobal(args.globalSlug, {
271
+ locale: args.locale,
272
+ draft: args.draft ?? false,
273
+ depth: args.depth
296
274
  });
297
- return result.docs?.[0] ?? null;
298
- });
275
+ return result ?? null;
276
+ } catch (error) {
277
+ console.warn("[WWW] queryGlobal failed", { globalSlug: args.globalSlug, locale: args.locale, error: String(error) });
278
+ return null;
279
+ }
299
280
  }
300
- async function queryGlobal({
301
- globalSlug,
302
- locale,
303
- draft = false,
304
- config
305
- }) {
306
- return withUnstableCache([globalSlug, locale, draft], [createCollectionCacheKey({ globalSlug, locale })], async () => {
307
- const payload = await getPayload({ config });
308
- try {
309
- return await payload.findGlobal({ slug: globalSlug, draft, locale });
310
- } catch (error) {
311
- console.warn("[WWW] queryGlobal failed", { globalSlug, locale, error: String(error) });
312
- return null;
313
- }
314
- });
281
+ async function queryAllDocs(args) {
282
+ "use cache";
283
+ cacheLife("weeks");
284
+ const { findIds, findDocByID } = await requireCacheHelpers();
285
+ const collection = args.collectionSlug;
286
+ const { ids } = await findIds(collection, { locale: args.locale });
287
+ if (ids.length === 0)
288
+ return [];
289
+ const docs = await Promise.all(ids.map((id) => findDocByID(collection, id, { locale: args.locale })));
290
+ return docs.filter((d) => d !== null);
315
291
  }
316
- async function queryAllDocs({
317
- collectionSlug,
318
- slugField = "slug",
319
- locale,
320
- config
321
- }) {
322
- return withUnstableCache([collectionSlug, slugField, locale], [createCollectionCacheKey({ collectionSlug, slug: "__all__", locale })], async () => {
323
- const payload = await getPayload({ config });
324
- const result = await payload.find({
325
- collection: collectionSlug,
326
- draft: false,
327
- limit: 1000,
328
- pagination: false,
329
- overrideAccess: false,
330
- select: { [slugField]: true },
331
- locale
332
- });
333
- return result.docs ?? [];
334
- });
292
+ async function queryDoc(args) {
293
+ if ("globalSlug" in args)
294
+ return queryGlobal(args);
295
+ return queryDocBySlug(args);
335
296
  }
336
- async function queryAllLocaleSlugs({
337
- collectionSlug,
338
- id,
339
- slugField = "slug",
340
- config
341
- }) {
342
- const payload = await getPayload({ config });
343
- const doc = await payload.findByID({
344
- collection: collectionSlug,
345
- id,
346
- locale: "all",
347
- select: { [slugField]: true }
297
+ async function queryAllLocaleSlugs(args) {
298
+ "use cache";
299
+ cacheLife("weeks");
300
+ const { findDocByID } = await requireCacheHelpers();
301
+ const slugField = args.slugField ?? "slug";
302
+ const doc = await findDocByID(args.collectionSlug, args.id, { locale: "all", select: { [slugField]: true } });
303
+ const localeMap = doc?.[slugField];
304
+ if (localeMap && typeof localeMap === "object")
305
+ return localeMap;
306
+ return null;
307
+ }
308
+ async function queryDocByID(args) {
309
+ "use cache";
310
+ cacheLife("weeks");
311
+ const { findDocByID } = await requireCacheHelpers();
312
+ return await findDocByID(args.collectionSlug, args.id, {
313
+ locale: args.locale,
314
+ draft: args.draft ?? false,
315
+ depth: args.depth
348
316
  });
349
- return doc?.[slugField];
350
317
  }
351
318
 
352
319
  // src/render/pages/createCollectionPageExports.tsx
353
320
  import { setRequestLocale } from "next-intl/server";
354
321
 
355
322
  // src/render/pages/utils/buildLocalizedPath.ts
323
+ function resolvePagePathPrefix(prefix, locale, routing) {
324
+ if (typeof prefix !== "object")
325
+ return prefix;
326
+ return prefix[locale] ?? prefix[routing.defaultLocale];
327
+ }
356
328
  function buildLocalizedPath(locale, prefix, slug, { routing }) {
357
329
  const path = slugToPath(slug);
330
+ const localePrefix = resolvePagePathPrefix(prefix, locale, routing);
358
331
  return `${routing.localePrefix === "never" ? "" : routing.localePrefix === "always" ? locale : routing.localePrefix === "as-needed" ? locale === routing.defaultLocale ? "" : "/" + locale : (() => {
359
332
  throw new Error("Unsupported locale prefix");
360
- })()}${prefix ? "/" + prefix : ""}${path ? "/" + path : ""}`;
333
+ })()}${localePrefix ? "/" + localePrefix : ""}${path ? "/" + path : ""}`;
361
334
  }
362
335
  function buildLocalizedPaths(localesSlug, pagePathPrefix, { routing }) {
363
336
  const blankIsHome = !localesSlug[routing.defaultLocale];
@@ -412,25 +385,31 @@ async function renderWWWDataModule(data, {
412
385
  import { jsx as jsx7, Fragment as Fragment2 } from "react/jsx-runtime";
413
386
  function createCollectionPageExports({
414
387
  slug: collectionSlug = "pages",
415
- config: configPromise,
388
+ _payloadConfig,
416
389
  importMap,
417
390
  routing,
418
391
  slugShape = "single"
419
392
  }, {
420
393
  getServerSideURL,
421
- pagePathPrefix
394
+ pagePathPrefix,
395
+ fallback
422
396
  }) {
397
+ seedPayloadCache({ config: _payloadConfig });
423
398
  const siteUrl = getServerSideURL();
424
399
  async function fetchDoc(locale, slug) {
425
400
  return queryDoc({
426
401
  slug,
427
402
  locale,
428
403
  collectionSlug
429
- }, {
430
- config: configPromise
431
404
  });
432
405
  }
433
- const default_ = async (props) => {
406
+ const default_ = (props) => /* @__PURE__ */ jsx7(React.Suspense, {
407
+ fallback: fallback ?? null,
408
+ children: /* @__PURE__ */ jsx7(CollectionDocument, {
409
+ ...props
410
+ })
411
+ });
412
+ const CollectionDocument = async (props) => {
434
413
  const params = await props.params;
435
414
  const locale = params.locale;
436
415
  if (!routing.locales.includes(locale)) {
@@ -444,22 +423,25 @@ function createCollectionPageExports({
444
423
  const { notFound } = await import("next/navigation");
445
424
  notFound();
446
425
  }
447
- const rendered = renderWWWDataModule(doc, { collectionSlug, config: configPromise, importMap }, { ...props, locale });
426
+ const rendered = renderWWWDataModule(doc, { collectionSlug, config: _payloadConfig, importMap }, { ...props, locale });
448
427
  return /* @__PURE__ */ jsx7(Fragment2, {
449
428
  children: rendered
450
429
  });
451
430
  };
452
431
  async function generateMetadata(props) {
453
432
  const params = await props.params;
454
- const locale = params.locale, slug = paramsSlugToSlug(params.slug, slugShape);
433
+ const locale = params.locale;
434
+ if (!routing.locales.includes(locale)) {
435
+ return {};
436
+ }
437
+ const slug = paramsSlugToSlug(params.slug, slugShape);
455
438
  const doc = await fetchDoc(locale, slug);
456
439
  const [localesSlug, siteDefaults] = await Promise.all([
457
440
  doc ? queryAllLocaleSlugs({
458
441
  id: doc.id,
459
- collectionSlug,
460
- config: configPromise
442
+ collectionSlug
461
443
  }) : Promise.resolve({}),
462
- createSiteDefaults({ config: configPromise, locale })
444
+ createSiteDefaults({ config: _payloadConfig, locale })
463
445
  ]);
464
446
  if (!doc)
465
447
  return {};
@@ -476,22 +458,23 @@ function createCollectionPageExports({
476
458
  return { ...meta, alternates };
477
459
  }
478
460
  async function generateStaticParams(props) {
479
- const locale = (await props.params).locale;
480
- const docs = await queryAllDocs({ locale, collectionSlug, config: configPromise });
481
- return docs.filter((doc) => typeof doc.slug === "string" && doc.slug.length > 0).map((doc) => ({ slug: slugToParamsSlug(doc.slug, slugShape) }));
461
+ await props.params;
462
+ const perLocaleEntries = await Promise.all(routing.locales.map(async (locale) => {
463
+ const docs = await queryAllDocs({ locale, collectionSlug });
464
+ return docs.filter((doc) => typeof doc.slug === "string" && doc.slug.length > 0).map((doc) => ({ locale, slug: slugToParamsSlug(doc.slug, slugShape) }));
465
+ }));
466
+ return perLocaleEntries.flat();
482
467
  }
483
468
  async function generateSitemap() {
484
469
  const locale = routing.defaultLocale;
485
470
  const docs = await queryAllDocs({
486
471
  collectionSlug,
487
- locale,
488
- config: configPromise
472
+ locale: routing.defaultLocale
489
473
  });
490
474
  return await Promise.all(docs.map(async (doc) => {
491
475
  const localesSlug = await queryAllLocaleSlugs({
492
476
  id: doc.id,
493
- collectionSlug,
494
- config: configPromise
477
+ collectionSlug
495
478
  }) ?? {};
496
479
  const alternates = buildAlternates(locale, localesSlug, pagePathPrefix, { routing, siteUrl });
497
480
  return {
@@ -502,7 +485,7 @@ function createCollectionPageExports({
502
485
  }));
503
486
  }
504
487
  generateSitemap.getServerSideURL = getServerSideURL;
505
- generateSitemap.pagePathPrefix = pagePathPrefix;
488
+ generateSitemap.pagePathPrefix = resolvePagePathPrefix(pagePathPrefix, routing.defaultLocale, routing);
506
489
  return {
507
490
  default: default_,
508
491
  generateMetadata,
@@ -526,13 +509,23 @@ ${sitemaps.map((url) => ` <sitemap><loc>${url}</loc></sitemap>`).join(`
526
509
  }
527
510
 
528
511
  // src/render/pages/createRootLayoutExports.tsx
529
- import"server-only";
512
+ import { Activity } from "react";
530
513
  import { setRequestLocale as setRequestLocale2 } from "next-intl/server";
531
514
  import { NextIntlClientProvider } from "next-intl";
532
515
  import { RootJsonLd } from "@justanarthur/payload-plugin-seo/root-jsonld";
516
+ import * as rootParams from "next/root-params";
533
517
  import { jsx as jsx8, jsxs as jsxs4 } from "react/jsx-runtime";
518
+ var readLocaleRootParam = rootParams.locale;
519
+ async function resolveLocale(props) {
520
+ if (readLocaleRootParam) {
521
+ const locale = await readLocaleRootParam();
522
+ if (locale)
523
+ return locale;
524
+ }
525
+ return (await props.params).locale;
526
+ }
534
527
  function createRootLayoutExports({
535
- config: configPromise,
528
+ _payloadConfig,
536
529
  importMap,
537
530
  routing
538
531
  }, {
@@ -540,9 +533,9 @@ function createRootLayoutExports({
540
533
  htmlAttrs,
541
534
  getServerSideURL
542
535
  } = {}) {
536
+ seedPayloadCache({ config: _payloadConfig });
543
537
  async function RootLayout(props) {
544
- const params = await props.params;
545
- const locale = params.locale;
538
+ const locale = await resolveLocale(props);
546
539
  if (!routing.locales.includes(locale)) {
547
540
  const { notFound } = await import("next/navigation");
548
541
  return notFound();
@@ -552,17 +545,17 @@ function createRootLayoutExports({
552
545
  header,
553
546
  footer
554
547
  ] = await Promise.all([
555
- queryDoc({ globalSlug: "header", locale }, { config: configPromise }),
556
- queryDoc({ globalSlug: "footer", locale }, { config: configPromise })
548
+ queryDoc({ globalSlug: "header", locale }),
549
+ queryDoc({ globalSlug: "footer", locale })
557
550
  ]);
558
- const renderedHeader = renderWWWDataModule(header, { collectionSlug: "header", configPath: "globals", config: configPromise, importMap }, { ...props, locale }), renderedFooter = renderWWWDataModule(footer, { collectionSlug: "footer", configPath: "globals", config: configPromise, importMap }, { ...props, locale });
551
+ const renderedHeader = renderWWWDataModule(header, { collectionSlug: "header", configPath: "globals", config: _payloadConfig, importMap }, { ...props, locale }), renderedFooter = renderWWWDataModule(footer, { collectionSlug: "footer", configPath: "globals", config: _payloadConfig, importMap }, { ...props, locale });
559
552
  const mergedHtmlAttrs = {
560
553
  lang: locale,
561
554
  suppressHydrationWarning: true,
562
555
  ...htmlAttrs?.(locale) ?? {}
563
556
  };
564
557
  const rootJsonLd = getServerSideURL ? /* @__PURE__ */ jsx8(RootJsonLd, {
565
- config: configPromise,
558
+ config: _payloadConfig,
566
559
  locale,
567
560
  getServerSideURL,
568
561
  locales: routing.locales
@@ -573,9 +566,13 @@ function createRootLayoutExports({
573
566
  children: /* @__PURE__ */ jsxs4(NextIntlClientProvider, {
574
567
  children: [
575
568
  rootJsonLd,
576
- renderedHeader,
569
+ /* @__PURE__ */ jsx8(Activity, {
570
+ children: renderedHeader
571
+ }),
577
572
  providers ? providers({ children: props.children, locale }) : props.children,
578
- renderedFooter
573
+ /* @__PURE__ */ jsx8(Activity, {
574
+ children: renderedFooter
575
+ })
579
576
  ]
580
577
  })
581
578
  })
@@ -592,12 +589,12 @@ import { RootJsonLd as RootJsonLd2 } from "@justanarthur/payload-plugin-seo/root
592
589
  var renderPages = { createCollectionPageExports, createRootLayoutExports, PagesPage, PostsPage, HeaderPage, FooterPage, RootJsonLd: RootJsonLd2 };
593
590
  var render_pages_default = renderPages;
594
591
  export {
595
- render_pages_default as default,
596
- createRootLayoutExports,
597
- createCollectionPageExports,
598
- RootJsonLd2 as RootJsonLd,
599
- PostsPage,
600
- PagesPage,
592
+ FooterPage,
601
593
  HeaderPage,
602
- FooterPage
594
+ PagesPage,
595
+ PostsPage,
596
+ RootJsonLd2 as RootJsonLd,
597
+ createCollectionPageExports,
598
+ createRootLayoutExports,
599
+ render_pages_default as default
603
600
  };
package/dist/sitemap.d.ts CHANGED
@@ -1,10 +1,26 @@
1
+ import { ReactNode as ReactNode2 } from "react";
2
+ /**
3
+ * URL segment a collection is mounted under. A string is used for every locale; a record
4
+ * localizes it (`{ en: 'posts', sk: 'prispevky' }`), so a host serving localized routes
5
+ * gets canonical/hreflang URLs that match the URLs it actually serves. Locales missing
6
+ * from the record fall back to the default locale.
7
+ */
8
+ type PagePathPrefix = string | Record<string, string>;
1
9
  type CreateCollectionPageExportsDeps<S extends string> = {
2
10
  getServerSideURL: () => string;
3
- pagePathPrefix?: string;
11
+ pagePathPrefix?: PagePathPrefix;
12
+ /** Rendered while the document streams. Defaults to nothing. */
13
+ fallback?: ReactNode2;
4
14
  };
5
15
  type GenerateSitemapPageExportsArgs = CreateCollectionPageExportsDeps<any>;
6
16
  declare function createSitemapFromCollections(...args: GenerateSitemapPageExportsArgs[]): () => Response;
17
+ import { SanitizedConfig as SanitizedConfig2 } from "payload";
18
+ type SeedPayloadCacheArgs = {
19
+ config: SanitizedConfig2 | Promise<SanitizedConfig2>;
20
+ };
21
+ declare function seedPayloadCache({ config }: SeedPayloadCacheArgs): void;
7
22
  declare const _default: {
8
23
  createSitemapFromCollections: typeof createSitemapFromCollections;
24
+ seedPayloadCache: typeof seedPayloadCache;
9
25
  };
10
- export { _default as default, createSitemapFromCollections };
26
+ export { createSitemapFromCollections, _default as default, seedPayloadCache };