next-client-cookies
Version:
SSR and client support for Next.js v13 cookies (app directory)
118 lines • 4.8 kB
JavaScript
;
"use client";
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var provider_exports = {};
__export(provider_exports, {
CookiesProvider: () => CookiesProvider,
SecureCookiesProvider: () => SecureCookiesProvider
});
module.exports = __toCommonJS(provider_exports);
var import_react = __toESM(require("react"));
var import_js_cookie = __toESM(require("js-cookie"));
var import_navigation = require("next/navigation");
var import_context = require("./context");
var import_secure = require("./secure");
const windowVarName = "__cookies_commands";
const CookiesProvider = /* @__PURE__ */ __name(({ value, children }) => {
const cookies = useCookieRecords(value);
return /* @__PURE__ */ import_react.default.createElement(import_context.Ctx.Provider, {
value: cookies
}, children);
}, "CookiesProvider");
const SecureCookiesProvider = /* @__PURE__ */ __name(({ value, children }) => {
const secureValue = (0, import_secure.useSecureCookies)(value);
const cookies = secureValue ? useCookieRecords(secureValue) : null;
return /* @__PURE__ */ import_react.default.createElement(import_context.Ctx.Provider, {
value: cookies
}, children);
}, "SecureCookiesProvider");
const useCookieRecords = /* @__PURE__ */ __name((value) => {
const insertedHTML = (0, import_react.useContext)(import_navigation.ServerInsertedHTMLContext);
const cookies = (0, import_react.useMemo)(() => {
const map = Object.fromEntries(value.map((c) => [
c.name,
c.value
]));
return {
get: (name) => name == null ? {
...map
} : map[name],
set: (...args) => {
insertedHTML?.(() => getCookieCommandHtml("set", ...args));
map[args[0]] = args[1];
},
remove: (...args) => {
insertedHTML?.(() => getCookieCommandHtml("remove", ...args));
delete map[args[0]];
}
};
}, [
value,
insertedHTML
]);
(0, import_react.useEffect)(() => {
const commands = window[windowVarName] || [];
if (!commands.length)
return;
for (const command of commands) {
runCookieCommand(command);
}
}, []);
return cookies;
}, "useCookieRecords");
const getCookieCommandHtml = /* @__PURE__ */ __name((...command) => /* @__PURE__ */ import_react.default.createElement("script", {
dangerouslySetInnerHTML: {
__html: `window.${windowVarName} = window.${windowVarName} || [];window.${windowVarName}.push(${JSON.stringify(command).replaceAll("</", "<\\/")});`
}
}), "getCookieCommandHtml");
const runCookieCommand = /* @__PURE__ */ __name((command) => {
if (typeof window === "undefined")
return;
switch (command[0]) {
case "set": {
import_js_cookie.default.set(command[1], command[2], command[3] && deserializeCookieAttributes(command[3]));
break;
}
case "remove": {
import_js_cookie.default.remove(command[1], command[2] && deserializeCookieAttributes(command[2]));
break;
}
}
}, "runCookieCommand");
const deserializeCookieAttributes = /* @__PURE__ */ __name((attributes) => ({
...attributes,
expires: typeof attributes.expires === "string" ? new Date(attributes.expires) : attributes.expires
}), "deserializeCookieAttributes");
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
CookiesProvider,
SecureCookiesProvider
});
//# sourceMappingURL=provider.js.map