@prismicio/next 1.3.6 → 1.5.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.
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
+ const Negotiator = require("negotiator");
4
+ const intlLocalematcher = require("@formatjs/intl-localematcher");
5
+ const createLocaleRedirect = async (config) => {
6
+ const repository = await config.client.getRepository();
7
+ const locales = repository.languages.map((language) => {
8
+ var _a;
9
+ return ((_a = config.localeOverrides) == null ? void 0 : _a[language.id]) ?? language.id;
10
+ });
11
+ const defaultLocale = locales[0];
12
+ const { pathname } = config.request.nextUrl;
13
+ const pathnameHasLocale = locales.some((locale2) => pathname.startsWith(`/${locale2}/`) || pathname === `/${locale2}`);
14
+ if (pathnameHasLocale) {
15
+ return;
16
+ }
17
+ let locale = defaultLocale;
18
+ const headers = {
19
+ "accept-language": config.request.headers.get("accept-language") ?? void 0
20
+ };
21
+ if (headers["accept-language"]) {
22
+ const languages = new Negotiator({ headers }).languages();
23
+ locale = intlLocalematcher.match(languages, locales, defaultLocale);
24
+ }
25
+ if (locale === defaultLocale && config.omitDefaultLocale) {
26
+ return;
27
+ }
28
+ config.request.nextUrl.pathname = `/${locale}${pathname}`;
29
+ return Response.redirect(config.request.nextUrl);
30
+ };
31
+ exports.createLocaleRedirect = createLocaleRedirect;
32
+ //# sourceMappingURL=createLocaleRedirect.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createLocaleRedirect.cjs","sources":["../../src/createLocaleRedirect.ts"],"sourcesContent":["import Negotiator from \"negotiator\";\nimport { match } from \"@formatjs/intl-localematcher\";\nimport * as prismic from \"@prismicio/client\";\n\nimport { NextRequestLike } from \"./types\";\n\nexport type CreateLocaleRedirectConfig = {\n\trequest: NextRequestLike;\n\tclient: Pick<prismic.Client, \"getRepository\">;\n\tlocaleOverrides?: Record<string, string>;\n\tomitDefaultLocale?: boolean;\n};\n\n/**\n * Creates a `Response` that redirects a request to the requester's preferred\n * locale. This function returns `undefined` if the request already contains a\n * locale.\n *\n * @returns A `Response` if the request should be redirected, `undefined`\n * otherwise.\n */\nexport const createLocaleRedirect = async (\n\tconfig: CreateLocaleRedirectConfig,\n): Promise<Response | undefined> => {\n\tconst repository = await config.client.getRepository();\n\n\tconst locales = repository.languages.map((language) => {\n\t\treturn config.localeOverrides?.[language.id] ?? language.id;\n\t});\n\tconst defaultLocale = locales[0];\n\n\tconst { pathname } = config.request.nextUrl;\n\tconst pathnameHasLocale = locales.some(\n\t\t(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`,\n\t);\n\n\tif (pathnameHasLocale) {\n\t\treturn;\n\t}\n\n\tlet locale = defaultLocale;\n\n\tconst headers = {\n\t\t\"accept-language\":\n\t\t\tconfig.request.headers.get(\"accept-language\") ?? undefined,\n\t};\n\n\tif (headers[\"accept-language\"]) {\n\t\tconst languages = new Negotiator({ headers }).languages();\n\t\tlocale = match(languages, locales, defaultLocale);\n\t}\n\n\tif (locale === defaultLocale && config.omitDefaultLocale) {\n\t\treturn;\n\t}\n\n\tconfig.request.nextUrl.pathname = `/${locale}${pathname}`;\n\n\treturn Response.redirect(config.request.nextUrl as URL);\n};\n"],"names":["locale","match"],"mappings":";;;;AAqBa,MAAA,uBAAuB,OACnC,WACkC;AAClC,QAAM,aAAa,MAAM,OAAO,OAAO,cAAa;AAEpD,QAAM,UAAU,WAAW,UAAU,IAAI,CAAC,aAAY;;AACrD,aAAO,YAAO,oBAAP,mBAAyB,SAAS,QAAO,SAAS;AAAA,EAAA,CACzD;AACK,QAAA,gBAAgB,QAAQ,CAAC;AAE/B,QAAM,EAAE,SAAa,IAAA,OAAO,QAAQ;AACpC,QAAM,oBAAoB,QAAQ,KACjC,CAACA,YAAW,SAAS,WAAW,IAAIA,OAAM,GAAG,KAAK,aAAa,IAAIA,OAAM,EAAE;AAG5E,MAAI,mBAAmB;AACtB;AAAA,EACA;AAED,MAAI,SAAS;AAEb,QAAM,UAAU;AAAA,IACf,mBACC,OAAO,QAAQ,QAAQ,IAAI,iBAAiB,KAAK;AAAA,EAAA;AAG/C,MAAA,QAAQ,iBAAiB,GAAG;AAC/B,UAAM,YAAY,IAAI,WAAW,EAAE,QAAS,CAAA,EAAE;AACrC,aAAAC,kBAAAA,MAAM,WAAW,SAAS,aAAa;AAAA,EAChD;AAEG,MAAA,WAAW,iBAAiB,OAAO,mBAAmB;AACzD;AAAA,EACA;AAED,SAAO,QAAQ,QAAQ,WAAW,IAAI,MAAM,GAAG,QAAQ;AAEvD,SAAO,SAAS,SAAS,OAAO,QAAQ,OAAc;AACvD;;"}
@@ -0,0 +1,17 @@
1
+ import * as prismic from "@prismicio/client";
2
+ import { NextRequestLike } from "./types";
3
+ export type CreateLocaleRedirectConfig = {
4
+ request: NextRequestLike;
5
+ client: Pick<prismic.Client, "getRepository">;
6
+ localeOverrides?: Record<string, string>;
7
+ omitDefaultLocale?: boolean;
8
+ };
9
+ /**
10
+ * Creates a `Response` that redirects a request to the requester's preferred
11
+ * locale. This function returns `undefined` if the request already contains a
12
+ * locale.
13
+ *
14
+ * @returns A `Response` if the request should be redirected, `undefined`
15
+ * otherwise.
16
+ */
17
+ export declare const createLocaleRedirect: (config: CreateLocaleRedirectConfig) => Promise<Response | undefined>;
@@ -0,0 +1,32 @@
1
+ import Negotiator from "negotiator";
2
+ import { match } from "@formatjs/intl-localematcher";
3
+ const createLocaleRedirect = async (config) => {
4
+ const repository = await config.client.getRepository();
5
+ const locales = repository.languages.map((language) => {
6
+ var _a;
7
+ return ((_a = config.localeOverrides) == null ? void 0 : _a[language.id]) ?? language.id;
8
+ });
9
+ const defaultLocale = locales[0];
10
+ const { pathname } = config.request.nextUrl;
11
+ const pathnameHasLocale = locales.some((locale2) => pathname.startsWith(`/${locale2}/`) || pathname === `/${locale2}`);
12
+ if (pathnameHasLocale) {
13
+ return;
14
+ }
15
+ let locale = defaultLocale;
16
+ const headers = {
17
+ "accept-language": config.request.headers.get("accept-language") ?? void 0
18
+ };
19
+ if (headers["accept-language"]) {
20
+ const languages = new Negotiator({ headers }).languages();
21
+ locale = match(languages, locales, defaultLocale);
22
+ }
23
+ if (locale === defaultLocale && config.omitDefaultLocale) {
24
+ return;
25
+ }
26
+ config.request.nextUrl.pathname = `/${locale}${pathname}`;
27
+ return Response.redirect(config.request.nextUrl);
28
+ };
29
+ export {
30
+ createLocaleRedirect
31
+ };
32
+ //# sourceMappingURL=createLocaleRedirect.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createLocaleRedirect.js","sources":["../../src/createLocaleRedirect.ts"],"sourcesContent":["import Negotiator from \"negotiator\";\nimport { match } from \"@formatjs/intl-localematcher\";\nimport * as prismic from \"@prismicio/client\";\n\nimport { NextRequestLike } from \"./types\";\n\nexport type CreateLocaleRedirectConfig = {\n\trequest: NextRequestLike;\n\tclient: Pick<prismic.Client, \"getRepository\">;\n\tlocaleOverrides?: Record<string, string>;\n\tomitDefaultLocale?: boolean;\n};\n\n/**\n * Creates a `Response` that redirects a request to the requester's preferred\n * locale. This function returns `undefined` if the request already contains a\n * locale.\n *\n * @returns A `Response` if the request should be redirected, `undefined`\n * otherwise.\n */\nexport const createLocaleRedirect = async (\n\tconfig: CreateLocaleRedirectConfig,\n): Promise<Response | undefined> => {\n\tconst repository = await config.client.getRepository();\n\n\tconst locales = repository.languages.map((language) => {\n\t\treturn config.localeOverrides?.[language.id] ?? language.id;\n\t});\n\tconst defaultLocale = locales[0];\n\n\tconst { pathname } = config.request.nextUrl;\n\tconst pathnameHasLocale = locales.some(\n\t\t(locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`,\n\t);\n\n\tif (pathnameHasLocale) {\n\t\treturn;\n\t}\n\n\tlet locale = defaultLocale;\n\n\tconst headers = {\n\t\t\"accept-language\":\n\t\t\tconfig.request.headers.get(\"accept-language\") ?? undefined,\n\t};\n\n\tif (headers[\"accept-language\"]) {\n\t\tconst languages = new Negotiator({ headers }).languages();\n\t\tlocale = match(languages, locales, defaultLocale);\n\t}\n\n\tif (locale === defaultLocale && config.omitDefaultLocale) {\n\t\treturn;\n\t}\n\n\tconfig.request.nextUrl.pathname = `/${locale}${pathname}`;\n\n\treturn Response.redirect(config.request.nextUrl as URL);\n};\n"],"names":["locale"],"mappings":";;AAqBa,MAAA,uBAAuB,OACnC,WACkC;AAClC,QAAM,aAAa,MAAM,OAAO,OAAO,cAAa;AAEpD,QAAM,UAAU,WAAW,UAAU,IAAI,CAAC,aAAY;;AACrD,aAAO,YAAO,oBAAP,mBAAyB,SAAS,QAAO,SAAS;AAAA,EAAA,CACzD;AACK,QAAA,gBAAgB,QAAQ,CAAC;AAE/B,QAAM,EAAE,SAAa,IAAA,OAAO,QAAQ;AACpC,QAAM,oBAAoB,QAAQ,KACjC,CAACA,YAAW,SAAS,WAAW,IAAIA,OAAM,GAAG,KAAK,aAAa,IAAIA,OAAM,EAAE;AAG5E,MAAI,mBAAmB;AACtB;AAAA,EACA;AAED,MAAI,SAAS;AAEb,QAAM,UAAU;AAAA,IACf,mBACC,OAAO,QAAQ,QAAQ,IAAI,iBAAiB,KAAK;AAAA,EAAA;AAG/C,MAAA,QAAQ,iBAAiB,GAAG;AAC/B,UAAM,YAAY,IAAI,WAAW,EAAE,QAAS,CAAA,EAAE;AACrC,aAAA,MAAM,WAAW,SAAS,aAAa;AAAA,EAChD;AAEG,MAAA,WAAW,iBAAiB,OAAO,mBAAmB;AACzD;AAAA,EACA;AAED,SAAO,QAAQ,QAAQ,WAAW,IAAI,MAAM,GAAG,QAAQ;AAEvD,SAAO,SAAS,SAAS,OAAO,QAAQ,OAAc;AACvD;"}
package/dist/index.cjs CHANGED
@@ -6,6 +6,7 @@ const PrismicPreview = require("./PrismicPreview.cjs");
6
6
  const PrismicNextLink = require("./PrismicNextLink.cjs");
7
7
  const enableAutoPreviews = require("./enableAutoPreviews.cjs");
8
8
  const redirectToPreviewURL = require("./redirectToPreviewURL.cjs");
9
+ const createLocaleRedirect = require("./createLocaleRedirect.cjs");
9
10
  const PrismicNextImage = require("./PrismicNextImage.cjs");
10
11
  const imgixLoader = require("./imgixLoader.cjs");
11
12
  exports.setPreviewData = setPreviewData.setPreviewData;
@@ -14,6 +15,7 @@ exports.PrismicPreview = PrismicPreview.PrismicPreview;
14
15
  exports.PrismicNextLink = PrismicNextLink.PrismicNextLink;
15
16
  exports.enableAutoPreviews = enableAutoPreviews.enableAutoPreviews;
16
17
  exports.redirectToPreviewURL = redirectToPreviewURL.redirectToPreviewURL;
18
+ exports.createLocaleRedirect = createLocaleRedirect.createLocaleRedirect;
17
19
  exports.PrismicNextImage = PrismicNextImage.PrismicNextImage;
18
20
  exports.imgixLoader = imgixLoader.imgixLoader;
19
21
  //# sourceMappingURL=index.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"index.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;"}
package/dist/index.d.ts CHANGED
@@ -10,6 +10,8 @@ export { enableAutoPreviews } from "./enableAutoPreviews";
10
10
  export type { EnableAutoPreviewsConfig } from "./enableAutoPreviews";
11
11
  export { redirectToPreviewURL } from "./redirectToPreviewURL";
12
12
  export type { RedirectToPreviewURLConfig, RedirectToPreviewURLRouteHandlerConfig, RedirectToPreviewURLAPIEndpointConfig, } from "./redirectToPreviewURL";
13
+ export { createLocaleRedirect } from "./createLocaleRedirect";
14
+ export type { CreateLocaleRedirectConfig } from "./createLocaleRedirect";
13
15
  export { PrismicNextImage } from "./PrismicNextImage";
14
16
  export type { PrismicNextImageProps } from "./PrismicNextImage";
15
17
  export { imgixLoader } from "./imgixLoader";
package/dist/index.js CHANGED
@@ -4,12 +4,14 @@ import { PrismicPreview } from "./PrismicPreview.js";
4
4
  import { PrismicNextLink } from "./PrismicNextLink.js";
5
5
  import { enableAutoPreviews } from "./enableAutoPreviews.js";
6
6
  import { redirectToPreviewURL } from "./redirectToPreviewURL.js";
7
+ import { createLocaleRedirect } from "./createLocaleRedirect.js";
7
8
  import { PrismicNextImage } from "./PrismicNextImage.js";
8
9
  import { imgixLoader } from "./imgixLoader.js";
9
10
  export {
10
11
  PrismicNextImage,
11
12
  PrismicNextLink,
12
13
  PrismicPreview,
14
+ createLocaleRedirect,
13
15
  enableAutoPreviews,
14
16
  exitPreview,
15
17
  imgixLoader,
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;"}
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;"}
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const version = "1.3.6";
3
+ const version = "1.5.0";
4
4
  exports.version = version;
5
5
  //# sourceMappingURL=package.json.cjs.map
@@ -1,4 +1,4 @@
1
- const version = "1.3.6";
1
+ const version = "1.5.0";
2
2
  export {
3
3
  version
4
4
  };
package/dist/types.d.ts CHANGED
@@ -44,6 +44,7 @@ export type NextRequestLike = {
44
44
  };
45
45
  url: string;
46
46
  nextUrl: {
47
+ pathname: string;
47
48
  searchParams: {
48
49
  get(name: string): string | null;
49
50
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@prismicio/next",
3
- "version": "1.3.6",
3
+ "version": "1.5.0",
4
4
  "description": "Helpers to integrate Prismic into Next.js apps",
5
5
  "keywords": [
6
6
  "typescript",
@@ -52,12 +52,15 @@
52
52
  "test": "npm run lint && npm run types && npm run unit && npm run build && npm run size"
53
53
  },
54
54
  "dependencies": {
55
- "imgix-url-builder": "^0.0.4"
55
+ "@formatjs/intl-localematcher": "^0.5.2",
56
+ "imgix-url-builder": "^0.0.4",
57
+ "negotiator": "^0.6.3"
56
58
  },
57
59
  "devDependencies": {
58
60
  "@prismicio/client": "^7.2.0",
59
61
  "@prismicio/mock": "^0.3.1",
60
62
  "@size-limit/preset-small-lib": "^9.0.0",
63
+ "@types/negotiator": "^0.6.3",
61
64
  "@types/react-test-renderer": "^18.0.2",
62
65
  "@typescript-eslint/eslint-plugin": "^6.7.2",
63
66
  "@typescript-eslint/parser": "^6.7.2",
@@ -71,7 +74,7 @@
71
74
  "eslint-plugin-tsdoc": "^0.2.17",
72
75
  "happy-dom": "^12.1.6",
73
76
  "memfs": "^4.4.0",
74
- "next": "^13.5.2",
77
+ "next": "^14.0.0",
75
78
  "node-fetch": "^3.3.2",
76
79
  "prettier": "^3.0.3",
77
80
  "prettier-plugin-jsdoc": "^1.0.2",
@@ -88,7 +91,7 @@
88
91
  },
89
92
  "peerDependencies": {
90
93
  "@prismicio/client": "^6 || ^7",
91
- "next": "^13.4.5",
94
+ "next": ">=13.4.5",
92
95
  "react": "^18"
93
96
  },
94
97
  "engines": {
@@ -0,0 +1,60 @@
1
+ import Negotiator from "negotiator";
2
+ import { match } from "@formatjs/intl-localematcher";
3
+ import * as prismic from "@prismicio/client";
4
+
5
+ import { NextRequestLike } from "./types";
6
+
7
+ export type CreateLocaleRedirectConfig = {
8
+ request: NextRequestLike;
9
+ client: Pick<prismic.Client, "getRepository">;
10
+ localeOverrides?: Record<string, string>;
11
+ omitDefaultLocale?: boolean;
12
+ };
13
+
14
+ /**
15
+ * Creates a `Response` that redirects a request to the requester's preferred
16
+ * locale. This function returns `undefined` if the request already contains a
17
+ * locale.
18
+ *
19
+ * @returns A `Response` if the request should be redirected, `undefined`
20
+ * otherwise.
21
+ */
22
+ export const createLocaleRedirect = async (
23
+ config: CreateLocaleRedirectConfig,
24
+ ): Promise<Response | undefined> => {
25
+ const repository = await config.client.getRepository();
26
+
27
+ const locales = repository.languages.map((language) => {
28
+ return config.localeOverrides?.[language.id] ?? language.id;
29
+ });
30
+ const defaultLocale = locales[0];
31
+
32
+ const { pathname } = config.request.nextUrl;
33
+ const pathnameHasLocale = locales.some(
34
+ (locale) => pathname.startsWith(`/${locale}/`) || pathname === `/${locale}`,
35
+ );
36
+
37
+ if (pathnameHasLocale) {
38
+ return;
39
+ }
40
+
41
+ let locale = defaultLocale;
42
+
43
+ const headers = {
44
+ "accept-language":
45
+ config.request.headers.get("accept-language") ?? undefined,
46
+ };
47
+
48
+ if (headers["accept-language"]) {
49
+ const languages = new Negotiator({ headers }).languages();
50
+ locale = match(languages, locales, defaultLocale);
51
+ }
52
+
53
+ if (locale === defaultLocale && config.omitDefaultLocale) {
54
+ return;
55
+ }
56
+
57
+ config.request.nextUrl.pathname = `/${locale}${pathname}`;
58
+
59
+ return Response.redirect(config.request.nextUrl as URL);
60
+ };
package/src/index.ts CHANGED
@@ -23,6 +23,9 @@ export type {
23
23
  RedirectToPreviewURLAPIEndpointConfig,
24
24
  } from "./redirectToPreviewURL";
25
25
 
26
+ export { createLocaleRedirect } from "./createLocaleRedirect";
27
+ export type { CreateLocaleRedirectConfig } from "./createLocaleRedirect";
28
+
26
29
  export { PrismicNextImage } from "./PrismicNextImage";
27
30
  export type { PrismicNextImageProps } from "./PrismicNextImage";
28
31
 
package/src/types.ts CHANGED
@@ -50,6 +50,7 @@ export type NextRequestLike = {
50
50
  };
51
51
  url: string;
52
52
  nextUrl: {
53
+ pathname: string;
53
54
  searchParams: {
54
55
  get(name: string): string | null;
55
56
  };