{"id":489174,"date":"2026-07-30T11:39:25","date_gmt":"2026-07-30T11:39:25","guid":{"rendered":"https:\/\/savepearlharbor.com\/?p=489174"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2026-07-30T11:39:25","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=489174","title":{"rendered":"Next.js, localStorage \u0432 App Router \u0431\u0435\u0437 hydration-\u043e\u0448\u0438\u0431\u043e\u043a"},"content":{"rendered":"<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p><code>localStorage<\/code> \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435. App Router \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c HTML \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0438 \u0434\u043b\u044f Client Component. \u041a\u043e\u0434 \u0441 \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u043e\u0439 <code>use client<\/code> \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u044d\u0444\u0444\u0435\u043a\u0442\u044b \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u043d\u043e \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0435\u043d\u0434\u0435\u0440 \u0432\u0441\u0451 \u0435\u0449\u0451 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0439\u0442\u0438 \u0431\u0435\u0437 <code>window<\/code> \u0438 <code>localStorage<\/code>. \u0418 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0434\u0432\u0430 \u0441\u0431\u043e\u044f. \u041f\u0440\u044f\u043c\u043e\u0435 \u0447\u0442\u0435\u043d\u0438\u0435 <code>localStorage<\/code> \u043f\u0430\u0434\u0430\u0435\u0442 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435. \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0447\u0435\u0440\u0435\u0437 <code>typeof window<\/code> \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0440\u0430\u0437\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0438 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435. \u0421\u0435\u0440\u0432\u0435\u0440 \u0441\u0442\u0440\u043e\u0438\u0442 \u043f\u0443\u0441\u0442\u043e\u0435 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0435, \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u0432\u0438\u0434\u0438\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u0435 id, React \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0440\u0430\u0437\u043d\u044b\u0435 \u0434\u0435\u0440\u0435\u0432\u044c\u044f \u043f\u0440\u0438 hydration.<\/p>\n<h3>\u041f\u0435\u0440\u0432\u044b\u0439 \u0440\u0435\u043d\u0434\u0435\u0440<\/h3>\n<p>Hydration \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 HTML, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u0448\u0451\u043b \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430. React \u0441\u0442\u0440\u043e\u0438\u0442 \u043f\u0435\u0440\u0432\u043e\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438. \u0420\u0430\u0437\u043c\u0435\u0442\u043a\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u043e\u0432\u043f\u0430\u0441\u0442\u044c. \u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043b\u043e\u043c\u0430\u0435\u0442\u0441\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0433\u043e \u0440\u0435\u043d\u0434\u0435\u0440\u0430.<\/p>\n<pre><code class=\"javascript\">\"use client\";import { useState } from \"react\";export default function FavoriteButton({ id }) {  const [ids, setIds] = useState(() =&gt; {    const raw = localStorage.getItem(\"favorites\");    return raw ? JSON.parse(raw) : [];  });  return (    &lt;button type=\"button\"&gt;      {ids.includes(id) ? \"\u0412 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u043c\" : \"\u0412 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0435\"}    &lt;\/button&gt;  );}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:87px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p><code>localStorage<\/code> \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u0432 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f. \u0421\u0435\u0440\u0432\u0435\u0440 \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 <code>typeof window !== \"undefined\"<\/code> \u0443\u0431\u0438\u0440\u0430\u0435\u0442 <code>ReferenceError<\/code>, \u043d\u043e \u043d\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u0443\u044e \u0440\u0430\u0437\u043c\u0435\u0442\u043a\u0443.<\/p>\n<pre><code class=\"javascript\">const [ids, setIds] = useState(() =&gt; {  if (typeof window === \"undefined\") return [];  const raw = localStorage.getItem(\"favorites\");  return raw ? JSON.parse(raw) : [];});<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u0432\u0435\u0440\u043d\u0451\u0442 \u043f\u0443\u0441\u0442\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432. \u0411\u0440\u0430\u0443\u0437\u0435\u0440 \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c \u0440\u0435\u043d\u0434\u0435\u0440\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0422\u0435\u043a\u0441\u0442 \u043a\u043d\u043e\u043f\u043a\u0438 \u0438\u043b\u0438 \u0447\u0438\u0441\u043b\u043e \u0432 badge \u0440\u0430\u0437\u043e\u0439\u0434\u0443\u0442\u0441\u044f.<\/p>\n<h3>\u0427\u0442\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 mount<\/h3>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439 HTML \u0438 \u0447\u0438\u0442\u0430\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043f\u043e\u0441\u043b\u0435 mount. \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0435 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0438 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435.<\/p>\n<pre><code class=\"javascript\">\"use client\";import { useEffect, useState } from \"react\";export default function FavoriteButton({ id }) {  const [isReady, setIsReady] = useState(false);  const [ids, setIds] = useState([]);  useEffect(() =&gt; {    const raw = localStorage.getItem(\"favorites\");    const parsed = raw ? JSON.parse(raw) : [];    setIds(Array.isArray(parsed) ? parsed : []);    setIsReady(true);  }, []);  if (!isReady) {    return &lt;button disabled&gt;...&lt;\/button&gt;;  }  return (    &lt;button type=\"button\"&gt;      {ids.includes(id) ? \"\u0412 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u043c\" : \"\u0412 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0435\"}    &lt;\/button&gt;  );}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0438\u0439 \u0440\u0435\u043d\u0434\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 disabled-\u043a\u043d\u043e\u043f\u043a\u0443. \u041f\u043e\u0441\u043b\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0447\u0438\u0442\u0430\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0438 \u043c\u0435\u043d\u044f\u0435\u0442 \u0442\u0435\u043a\u0441\u0442. \u0422\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 SSR \u0434\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430. \u041c\u0435\u0436\u0434\u0443 \u043f\u0435\u0440\u0432\u044b\u043c HTML \u0438 \u0447\u0442\u0435\u043d\u0438\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u0432\u0438\u0434\u0435\u043d placeholder. \u0414\u043b\u044f badge \u043c\u043e\u0436\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c <code>null<\/code>, \u0434\u043b\u044f \u043a\u043d\u043e\u043f\u043a\u0438 \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c disabled-\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0434\u043b\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0431\u043b\u043e\u043a \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438.<\/p>\n<h3>Client-only \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e<\/h3>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u0442 SSR \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 <code>localStorage<\/code>. \u0412 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 <a href=\"https:\/\/goods-finder-js.vercel.app\/\" rel=\"noopener noreferrer nofollow\">Goods Finder<\/a> \u0442\u0430\u043a \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u044e\u0442\u0441\u044f \u043a\u043d\u043e\u043f\u043a\u0430 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e, \u0441\u043f\u0438\u0441\u043e\u043a \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e, \u0438\u0441\u0442\u043e\u0440\u0438\u044f \u0438 \u0441\u0447\u0451\u0442\u0447\u0438\u043a\u0438 \u0432 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438. <code>ssr: false<\/code> \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 Client Component. \u0412 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u043c App Router \u0442\u0430\u043a\u0430\u044f \u043e\u043f\u0446\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0430\u043c.<\/p>\n<pre><code class=\"javascript\">\/\/ src\/components\/goods\/FavoriteButtonLoader.js\"use client\";import dynamic from \"next\/dynamic\";const FavoriteButton = dynamic(() =&gt; import(\".\/FavoriteButton\"), {  ssr: false,  loading: () =&gt; (    &lt;button type=\"button\" disabled&gt;      ...    &lt;\/button&gt;  ),});export default function FavoriteButtonLoader(props) {  return &lt;FavoriteButton {...props} \/&gt;;}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435. Lazy initializer \u0447\u0438\u0442\u0430\u0435\u0442 <code>localStorage<\/code> \u0443\u0436\u0435 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435.<\/p>\n<pre><code class=\"javascript\">\/\/ src\/components\/goods\/FavoriteButton.js\"use client\";import { useState } from \"react\";import {  isFavoriteId,  toggleFavoriteId,} from \"@\/app\/_storage\/goodsStorage\";export default function FavoriteButton({ id }) {  const [isFav, setIsFav] = useState(() =&gt; isFavoriteId(id));  function handleClick() {    const nextIsFav = toggleFavoriteId(id);    setIsFav(nextIsFav);  }  return (    &lt;button type=\"button\" onClick={handleClick}&gt;      {isFav ? \"\u0412 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u043c\" : \"\u0412 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0435\"}    &lt;\/button&gt;  );}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0438\u0437 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430. Loader \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 loading-\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0433\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430.<\/p>\n<h3>\u0421\u043b\u043e\u0439 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f<\/h3>\n<p>\u0412\u044b\u0437\u043e\u0432\u044b <code>getItem<\/code>, <code>setItem<\/code>, <code>JSON.parse<\/code> \u0438 \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043e\u0431\u0440\u0430\u043d\u044b \u0432 <code>goodsStorage.js<\/code>, \u043d\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044f\u0441\u044c \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043d\u043e\u043f\u043a\u0435:<\/p>\n<pre><code class=\"javascript\">\/\/ src\/app\/_storage\/goodsStorage.jsexport const FAVORITES_IDS_KEY = \"goods-finder:favorites\";export const HISTORY_KEY = \"goods-finder:history\";export const GOODS_STORAGE_EVENT = \"goods-finder:storage\";export const HISTORY_LIMIT = 20;function normalizePositiveInt(value) {  const n = Number(value);  if (!Number.isInteger(n)) return null;  if (n &lt;= 0) return null;  return n;}export function readFavoriteIds() {  try {    const raw = localStorage.getItem(FAVORITES_IDS_KEY);    if (!raw) return [];    const parsed = JSON.parse(raw);    if (!Array.isArray(parsed)) return [];    return parsed      .map(normalizePositiveInt)      .filter(value =&gt; value !== null);  } catch {    return [];  }}export function writeFavoriteIds(ids) {  try {    const safe = Array.from(      new Set(        ids          .map(normalizePositiveInt)          .filter(value =&gt; value !== null)      )    );    localStorage.setItem(FAVORITES_IDS_KEY, JSON.stringify(safe));  } catch {    \/\/ Storage \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0438\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435 \u0437\u0430\u043f\u0438\u0441\u0430\u043b\u0438\u0441\u044c.  }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0427\u0442\u0435\u043d\u0438\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 id. \u0417\u0430\u043f\u0438\u0441\u044c \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0434\u0443\u0431\u043b\u0438. \u041e\u0448\u0438\u0431\u043a\u0430 JSON \u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043d\u0435 \u0440\u043e\u043d\u044f\u044e\u0442 UI.<\/p>\n<p>\u0412 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043e\u0441\u0442\u043e\u0432\u0435\u0440\u043d\u044b\u043c\u0438. \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043c\u043e\u0436\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 DevTools. \u0421\u0442\u0430\u0440\u044b\u0439 \u043a\u043e\u0434 \u043c\u043e\u0433 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u0444\u043e\u0440\u043c\u0430\u0442. \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430 \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435. \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e id \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 storage-\u0441\u043b\u043e\u044f.<\/p>\n<h3>\u0421\u043e\u0431\u044b\u0442\u0438\u0435 storage<\/h3>\n<p><code>storage<\/code> \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u043e\u0434\u043d\u043e\u0433\u043e origin. \u0421\u043e\u0431\u044b\u0442\u0438\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0432 \u0434\u0440\u0443\u0433\u0438\u0435 \u0432\u043a\u043b\u0430\u0434\u043a\u0438 \u0438 \u043e\u043a\u043d\u0430. \u0412\u043a\u043b\u0430\u0434\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u0437\u0432\u0430\u043b\u0430 <code>localStorage.setItem<\/code>, \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u044f <code>storage<\/code> \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442. \u0418\u0437-\u0437\u0430 \u044d\u0442\u043e\u0433\u043e \u043e\u0434\u0438\u043d listener \u043d\u0430 <code>storage<\/code> \u043d\u0435 \u043e\u0431\u043d\u043e\u0432\u0438\u0442 badge \u0432 \u0442\u043e\u0439 \u0436\u0435 \u0432\u043a\u043b\u0430\u0434\u043a\u0435. \u041a\u043d\u043e\u043f\u043a\u0430 \u0437\u0430\u043f\u0438\u0448\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 id, \u0430 \u0441\u0447\u0451\u0442\u0447\u0438\u043a \u0432 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0436\u043d\u0438\u043c \u0434\u043e \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f. \u041f\u0440\u043e\u0435\u043a\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438.<\/p>\n<pre><code class=\"javascript\">export const GOODS_STORAGE_EVENT = \"goods-finder:storage\";function emitGoodsStorageEvent() {  if (typeof window === \"undefined\") return;  window.dispatchEvent(new Event(GOODS_STORAGE_EVENT));}export function writeFavoriteIds(ids) {  try {    const safe = Array.from(new Set(ids));    localStorage.setItem(FAVORITES_IDS_KEY, JSON.stringify(safe));    emitGoodsStorageEvent();  } catch {    \/\/ ignore  }}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0432\u043a\u043b\u0430\u0434\u043a\u0435. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435 <code>storage<\/code> \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u043a\u043b\u0430\u0434\u043e\u043a. \u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u043e\u0431\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f.<\/p>\n<pre><code class=\"javascript\">\"use client\";import { useEffect, useState } from \"react\";import {  GOODS_STORAGE_EVENT,  readFavoriteIds,} from \"@\/app\/_storage\/goodsStorage\";export default function FavoritesBadge() {  const [count, setCount] = useState(() =&gt; readFavoriteIds().length);  useEffect(() =&gt; {    const update = () =&gt; setCount(readFavoriteIds().length);    window.addEventListener(GOODS_STORAGE_EVENT, update);    window.addEventListener(\"storage\", update);    return () =&gt; {      window.removeEventListener(GOODS_STORAGE_EVENT, update);      window.removeEventListener(\"storage\", update);    };  }, []);  if (!count) return null;  return &lt;span&gt;{count}&lt;\/span&gt;;}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041e\u0434\u0438\u043d \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043f\u0435\u0440\u0435\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u043e\u0438\u0445 \u0441\u043e\u0431\u044b\u0442\u0438\u0439.<\/p>\n<h3>\u0418\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0435<\/h3>\n<p>\u0418\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u0430\u0441\u0441\u0438\u0432 id. \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435, \u0446\u0435\u043d\u0430 \u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u0435.<\/p>\n<pre><code class=\"javascript\">[7, 31, 4]<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u0447\u0438\u0442\u0430\u0435\u0442 id \u0438\u0437 <code>localStorage<\/code>, \u0437\u0430\u0442\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u043a\u0430\u0440\u0442\u043e\u0447\u043a\u0438 \u0447\u0435\u0440\u0435\u0437 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 API.<\/p>\n<pre><code class=\"javascript\">async function loadFavoriteItems(ids) {  const results = await Promise.all(    ids.map(async id =&gt; {      const res = await fetch(`\/api\/goods\/${id}`, {        cache: \"no-store\",      });      const data = await res.json().catch(() =&gt; null);      if (!res.ok || !data?.ok) {        return { ok: false, id };      }      return { ok: true, item: data.item };    })  );  return results    .filter(result =&gt; result.ok)    .map(result =&gt; result.item);}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p><code>localStorage<\/code> \u0445\u0440\u0430\u043d\u0438\u0442 \u0432\u044b\u0431\u043e\u0440 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. API \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0442\u043e\u0432\u0430\u0440\u043e\u0432.<\/p>\n<p>\u0412 \u0441\u043f\u0438\u0441\u043a\u0435 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f. \u041f\u0435\u0440\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 id \u0438\u0437 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430. \u0412\u0442\u043e\u0440\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0435 \u043a\u0430\u0440\u0442\u043e\u0447\u043a\u0438.<\/p>\n<pre><code class=\"javascript\">const [ids, setIds] = useState(() =&gt; readFavoriteIds());const [items, setItems] = useState([]);useEffect(() =&gt; {  let cancelled = false;  async function run() {    if (ids.length === 0) {      setItems([]);      return;    }    const loaded = await loadFavoriteItems(ids);    if (!cancelled) setItems(loaded);  }  run();  return () =&gt; {    cancelled = true;  };}, [ids]);<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0424\u043b\u0430\u0433 <code>cancelled<\/code> \u043d\u0435 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043f\u043e\u0441\u043b\u0435 cleanup \u044d\u0444\u0444\u0435\u043a\u0442\u0430.<\/p>\n<h3>\u0418\u0441\u0442\u043e\u0440\u0438\u044f \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u043e\u0432<\/h3>\n<p>\u0418\u0441\u0442\u043e\u0440\u0438\u044f \u0445\u0440\u0430\u043d\u0438\u0442 <code>id<\/code>, \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0438 \u0432\u0440\u0435\u043c\u044f \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430. \u0417\u0430\u043f\u0438\u0441\u044c \u0441 \u0442\u0435\u043c \u0436\u0435 id \u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0441\u044f, \u0437\u0430\u0442\u0435\u043c \u043d\u043e\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0441\u0442\u0430\u0432\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e. \u0414\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0430 \u0434\u0432\u0430\u0434\u0446\u0430\u0442\u044c\u044e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438.<\/p>\n<pre><code class=\"javascript\">export function addToHistory(id, title) {  const safeId = normalizePositiveInt(id);  if (!safeId) return;  const safeTitle = normalizeTitle(title);  const now = new Date().toISOString();  const current = readHistoryRaw();  const filtered = current.filter(item =&gt; {    if (!item || typeof item !== \"object\") return true;    return Number(item.id) !== safeId;  });  const next = [    { id: safeId, title: safeTitle, at: now },    ...filtered,  ].slice(0, HISTORY_LIMIT);  writeHistoryRaw(next);}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u043a\u0430\u0440\u0442\u043e\u0447\u043a\u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e Client Component \u0431\u0435\u0437 UI.<\/p>\n<pre><code class=\"javascript\">\"use client\";import { useEffect } from \"react\";import { addToHistory } from \"@\/app\/_storage\/goodsStorage\";export default function HistoryPing({ id, title }) {  useEffect(() =&gt; {    addToHistory(id, title);  }, [id, title]);  return null;}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u042d\u0444\u0444\u0435\u043a\u0442 \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u043f\u043e\u0441\u043b\u0435 mount \u043a\u0430\u0440\u0442\u043e\u0447\u043a\u0438.<\/p>\n<h3>\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432<\/h3>\n<p>\u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0442\u043e\u0432\u0430\u0440\u0430 \u043c\u043e\u0436\u0435\u0442 \u043e\u0441\u0442\u0430\u0442\u044c\u0441\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0439. \u0414\u0430\u043d\u043d\u044b\u0435 \u0442\u043e\u0432\u0430\u0440\u0430 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435. \u041a\u043d\u043e\u043f\u043a\u0430 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0438\u0441\u0442\u043e\u0440\u0438\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u044e\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0438\u043c\u0438 \u043e\u0441\u0442\u0440\u043e\u0432\u043a\u0430\u043c\u0438. <\/p>\n<pre><code class=\"javascript\">export default async function GoodsDetailsPage({ params }) {  const { id } = await params;  const item = await getProductById(id);  return (    &lt;article&gt;      &lt;HistoryPing id={item.id} title={item.title} \/&gt;      &lt;h1&gt;{item.title}&lt;\/h1&gt;      &lt;FavoriteButtonLoader id={item.id} \/&gt;    &lt;\/article&gt;  );}<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0421\u0435\u0440\u0432\u0435\u0440 \u043e\u0442\u0432\u0435\u0447\u0430\u0435\u0442 \u0437\u0430 \u0434\u0430\u043d\u043d\u044b\u0435 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u044b. Browser-only \u043a\u043e\u0434 \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0432 <code>HistoryPing<\/code> \u0438 <code>FavoriteButtonLoader<\/code>.<\/p>\n<h3>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430<\/h3>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 \u0447\u0438\u0441\u0442\u043e\u0433\u043e \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430. \u041e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u043c \u043a\u0430\u0440\u0442\u043e\u0447\u043a\u0443 \u0442\u043e\u0432\u0430\u0440\u0430, \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c id \u0432 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0435, \u043f\u0435\u0440\u0435\u0445\u043e\u0434\u0438\u043c \u043d\u0430 \u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0443 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e. \u041f\u043e\u0441\u043b\u0435 reload \u0437\u0430\u043f\u0438\u0441\u044c \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f. \u041f\u043e\u0441\u043b\u0435 \u0443\u0434\u0430\u043b\u0435\u043d\u0438\u044f id \u043a\u043d\u043e\u043f\u043a\u0430 \u0438 badge \u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0432\u043a\u043b\u0430\u0434\u043a\u0435.<\/p>\n<p>\u0412\u0442\u043e\u0440\u0430\u044f \u0432\u043a\u043b\u0430\u0434\u043a\u0430 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043d\u0430 \u0442\u043e\u043c \u0436\u0435 origin. \u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u0432 \u043f\u0435\u0440\u0432\u043e\u0439 \u0432\u043a\u043b\u0430\u0434\u043a\u0435 \u0434\u043e\u043b\u0436\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u0438\u0442\u044c \u0441\u0447\u0451\u0442\u0447\u0438\u043a \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0447\u0435\u0440\u0435\u0437 <code>storage<\/code>.  <\/p>\n<p>\u041f\u043e\u0432\u0440\u0435\u0436\u0434\u0451\u043d\u043d\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u0442\u0441\u044f \u0447\u0435\u0440\u0435\u0437 DevTools.<\/p>\n<pre><code class=\"javascript\">localStorage.setItem(\"goods-finder:favorites\", \"broken-json\");<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043f\u0443\u0441\u0442\u043e\u0439 \u0441\u043f\u0438\u0441\u043e\u043a \u0438 \u043d\u0435 \u043f\u0430\u0434\u0430\u0435\u0442.<\/p>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c production-\u0441\u0431\u043e\u0440\u043a\u0443 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e.<\/p>\n<pre><code class=\"javascript\">npm run buildnpm start<\/code><div class=\"code-explainer\"><a href=\"https:\/\/sourcecraft.dev\/\" class=\"tm-button code-explainer__link\" style=\"visibility: hidden;\"><img style=\"width:14px;height:14px;object-fit:cover;object-position:left;\"\/><\/a><\/div><\/pre>\n<p>\u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u0441 <code>ssr: false<\/code> \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 Client Component loaders. \u0412 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u043d\u0435\u0442 hydration-\u043e\u0448\u0438\u0431\u043e\u043a. \u041f\u0435\u0440\u0432\u044b\u0439 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439 HTML \u043d\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f <code>localStorage<\/code>.<\/p>\n<p> \u200b<\/p>\n<\/div>\n<p>\u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/articles\/1064926\/\">https:\/\/habr.com\/ru\/articles\/1064926\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>localStorage \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435. App Router \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c HTML \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0438 \u0434\u043b\u044f Client Component. \u041a\u043e\u0434 \u0441 \u0434\u0438\u0440\u0435\u043a\u0442\u0438\u0432\u043e\u0439 use client \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u044d\u0444\u0444\u0435\u043a\u0442\u044b \u0438 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438 \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u043d\u043e \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0435\u043d\u0434\u0435\u0440 \u0432\u0441\u0451 \u0435\u0449\u0451 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0439\u0442\u0438 \u0431\u0435\u0437 window \u0438 localStorage. \u0418 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0434\u0432\u0430 \u0441\u0431\u043e\u044f. \u041f\u0440\u044f\u043c\u043e\u0435 \u0447\u0442\u0435\u043d\u0438\u0435 localStorage \u043f\u0430\u0434\u0430\u0435\u0442 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435. \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u0447\u0435\u0440\u0435\u0437 typeof window \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0440\u0430\u0437\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0438 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435. \u0421\u0435\u0440\u0432\u0435\u0440 \u0441\u0442\u0440\u043e\u0438\u0442 \u043f\u0443\u0441\u0442\u043e\u0435 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0435, \u0431\u0440\u0430\u0443\u0437\u0435\u0440 \u0432\u0438\u0434\u0438\u0442 \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u0435 id, React \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442 \u0440\u0430\u0437\u043d\u044b\u0435 \u0434\u0435\u0440\u0435\u0432\u044c\u044f \u043f\u0440\u0438 hydration.\u041f\u0435\u0440\u0432\u044b\u0439 \u0440\u0435\u043d\u0434\u0435\u0440Hydration \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442\u0441\u044f \u0441 HTML, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0440\u0438\u0448\u0451\u043b \u0441 \u0441\u0435\u0440\u0432\u0435\u0440\u0430. React \u0441\u0442\u0440\u043e\u0438\u0442 \u043f\u0435\u0440\u0432\u043e\u0435 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e \u0438 \u043f\u0440\u0438\u0432\u044f\u0437\u044b\u0432\u0430\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438. \u0420\u0430\u0437\u043c\u0435\u0442\u043a\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u0441\u043e\u0432\u043f\u0430\u0441\u0442\u044c. \u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043b\u043e\u043c\u0430\u0435\u0442\u0441\u044f \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0433\u043e \u0440\u0435\u043d\u0434\u0435\u0440\u0430.&#187;use client&#187;;import { useState } from &#171;react&#187;;export default function FavoriteButton({ id }) {  const [ids, setIds] = useState(() =&gt; {    const raw = localStorage.getItem(&#171;favorites&#187;);    return raw ? JSON.parse(raw) : [];  });  return (    &lt;button type=&#187;button&#187;&gt;      {ids.includes(id) ? &#171;\u0412 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u043c&#187; : &#171;\u0412 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0435&#187;}    &lt;\/button&gt;  );}localStorage \u0447\u0438\u0442\u0430\u0435\u0442\u0441\u044f \u0432 \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0442\u043e\u0440\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f. \u0421\u0435\u0440\u0432\u0435\u0440 \u043d\u0435 \u0437\u043d\u0430\u0435\u0442 \u0442\u0430\u043a\u043e\u0433\u043e \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430. \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 typeof window !== &#171;undefined&#187; \u0443\u0431\u0438\u0440\u0430\u0435\u0442 ReferenceError, \u043d\u043e \u043d\u0435 \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u0443\u0435\u0442 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u0443\u044e \u0440\u0430\u0437\u043c\u0435\u0442\u043a\u0443.const [ids, setIds] = useState(() =&gt; {  if (typeof window === &#171;undefined&#187;) return [];  const raw = localStorage.getItem(&#171;favorites&#187;);  return raw ? JSON.parse(raw) : [];});\u0421\u0435\u0440\u0432\u0435\u0440 \u0432\u0435\u0440\u043d\u0451\u0442 \u043f\u0443\u0441\u0442\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432. \u0411\u0440\u0430\u0443\u0437\u0435\u0440 \u043d\u0430 \u043f\u0435\u0440\u0432\u043e\u043c \u0440\u0435\u043d\u0434\u0435\u0440\u0435 \u043c\u043e\u0436\u0435\u0442 \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u0441\u043e\u0445\u0440\u0430\u043d\u0451\u043d\u043d\u044b\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f. \u0422\u0435\u043a\u0441\u0442 \u043a\u043d\u043e\u043f\u043a\u0438 \u0438\u043b\u0438 \u0447\u0438\u0441\u043b\u043e \u0432 badge \u0440\u0430\u0437\u043e\u0439\u0434\u0443\u0442\u0441\u044f.\u0427\u0442\u0435\u043d\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 mount\u041f\u0435\u0440\u0432\u044b\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u044b\u0439 HTML \u0438 \u0447\u0438\u0442\u0430\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043f\u043e\u0441\u043b\u0435 mount. \u041d\u0430\u0447\u0430\u043b\u044c\u043d\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u043e\u0435 \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 \u0438 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435.&#187;use client&#187;;import { useEffect, useState } from &#171;react&#187;;export default function FavoriteButton({ id }) {  const [isReady, setIsReady] = useState(false);  const [ids, setIds] = useState([]);  useEffect(() =&gt; {    const raw = localStorage.getItem(&#171;favorites&#187;);    const parsed = raw ? JSON.parse(raw) : [];    setIds(Array.isArray(parsed) ? parsed : []);    setIsReady(true);  }, []);  if (!isReady) {    return &lt;button disabled&gt;&#8230;&lt;\/button&gt;;  }  return (    &lt;button type=&#187;button&#187;&gt;      {ids.includes(id) ? &#171;\u0412 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u043c&#187; : &#171;\u0412 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0435&#187;}    &lt;\/button&gt;  );}\u0421\u0435\u0440\u0432\u0435\u0440 \u0438 \u043f\u0435\u0440\u0432\u044b\u0439 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0438\u0439 \u0440\u0435\u043d\u0434\u0435\u0440 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u044e\u0442 disabled-\u043a\u043d\u043e\u043f\u043a\u0443. \u041f\u043e\u0441\u043b\u0435 \u044d\u0444\u0444\u0435\u043a\u0442\u0430 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0447\u0438\u0442\u0430\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u0438 \u043c\u0435\u043d\u044f\u0435\u0442 \u0442\u0435\u043a\u0441\u0442. \u0422\u0430\u043a\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 SSR \u0434\u043b\u044f \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u0430. \u041c\u0435\u0436\u0434\u0443 \u043f\u0435\u0440\u0432\u044b\u043c HTML \u0438 \u0447\u0442\u0435\u043d\u0438\u0435\u043c \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430 \u0432\u0438\u0434\u0435\u043d placeholder. \u0414\u043b\u044f badge \u043c\u043e\u0436\u043d\u043e \u0432\u0435\u0440\u043d\u0443\u0442\u044c null, \u0434\u043b\u044f \u043a\u043d\u043e\u043f\u043a\u0438 \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c disabled-\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435, \u0434\u043b\u044f \u0441\u043f\u0438\u0441\u043a\u0430 \u043f\u043e\u043a\u0430\u0437\u0430\u0442\u044c \u0431\u043b\u043e\u043a \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438.Client-only \u043f\u043e\u0434\u0434\u0435\u0440\u0435\u0432\u043e\u0412\u0442\u043e\u0440\u043e\u0439 \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u043e\u0442\u043a\u043b\u044e\u0447\u0430\u0435\u0442 SSR \u0434\u043b\u044f \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0437\u0430\u0432\u0438\u0441\u0438\u0442 \u043e\u0442 localStorage. \u0412 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 Goods Finder \u0442\u0430\u043a \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u044e\u0442\u0441\u044f \u043a\u043d\u043e\u043f\u043a\u0430 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e, \u0441\u043f\u0438\u0441\u043e\u043a \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e, \u0438\u0441\u0442\u043e\u0440\u0438\u044f \u0438 \u0441\u0447\u0451\u0442\u0447\u0438\u043a\u0438 \u0432 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438. ssr: false \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0432 Client Component. \u0412 \u0430\u043a\u0442\u0443\u0430\u043b\u044c\u043d\u043e\u043c App Router \u0442\u0430\u043a\u0430\u044f \u043e\u043f\u0446\u0438\u044f \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0430\u043c.\/\/ src\/components\/goods\/FavoriteButtonLoader.js&#187;use client&#187;;import dynamic from &#171;next\/dynamic&#187;;const FavoriteButton = dynamic(() =&gt; import(&#171;.\/FavoriteButton&#187;), {  ssr: false,  loading: () =&gt; (    &lt;button type=&#187;button&#187; disabled&gt;      &#8230;    &lt;\/button&gt;  ),});export default function FavoriteButtonLoader(props) {  return &lt;FavoriteButton {&#8230;props} \/&gt;;}\u0412\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u043d\u0435 \u0440\u0435\u043d\u0434\u0435\u0440\u0438\u0442\u0441\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435. Lazy initializer \u0447\u0438\u0442\u0430\u0435\u0442 localStorage \u0443\u0436\u0435 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435.\/\/ src\/components\/goods\/FavoriteButton.js&#187;use client&#187;;import { useState } from &#171;react&#187;;import {  isFavoriteId,  toggleFavoriteId,} from &#171;@\/app\/_storage\/goodsStorage&#187;;export default function FavoriteButton({ id }) {  const [isFav, setIsFav] = useState(() =&gt; isFavoriteId(id));  function handleClick() {    const nextIsFav = toggleFavoriteId(id);    setIsFav(nextIsFav);  }  return (    &lt;button type=&#187;button&#187; onClick={handleClick}&gt;      {isFav ? &#171;\u0412 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u043c&#187; : &#171;\u0412 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0435&#187;}    &lt;\/button&gt;  );}\u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043d\u0430\u0447\u0438\u043d\u0430\u0435\u0442 \u0440\u0430\u0431\u043e\u0442\u0443 \u0441 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0438\u0437 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0430. Loader \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 loading-\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e \u0437\u0430\u0433\u0440\u0443\u0437\u043a\u0438 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0435\u0433\u043e \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u0430.\u0421\u043b\u043e\u0439 \u0445\u0440\u0430\u043d\u0435\u043d\u0438\u044f\u0412\u044b\u0437\u043e\u0432\u044b getItem, setItem, JSON.parse \u0438 \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0441\u043e\u0431\u0440\u0430\u043d\u044b \u0432 goodsStorage.js, \u043d\u0435 \u043f\u043e\u0432\u0442\u043e\u0440\u044f\u044f\u0441\u044c \u0432 \u043a\u0430\u0436\u0434\u043e\u0439 \u043a\u043d\u043e\u043f\u043a\u0435:\/\/ src\/app\/_storage\/goodsStorage.jsexport const FAVORITES_IDS_KEY = &#171;goods-finder:favorites&#187;;export const HISTORY_KEY = &#171;goods-finder:history&#187;;export const GOODS_STORAGE_EVENT = &#171;goods-finder:storage&#187;;export const HISTORY_LIMIT = 20;function normalizePositiveInt(value) {  const n = Number(value);  if (!Number.isInteger(n)) return null;  if (n &lt;= 0) return null;  return n;}export function readFavoriteIds() {  try {    const raw = localStorage.getItem(FAVORITES_IDS_KEY);    if (!raw) return [];    const parsed = JSON.parse(raw);    if (!Array.isArray(parsed)) return [];    return parsed      .map(normalizePositiveInt)      .filter(value =&gt; value !== null);  } catch {    return [];  }}export function writeFavoriteIds(ids) {  try {    const safe = Array.from(      new Set(        ids          .map(normalizePositiveInt)          .filter(value =&gt; value !== null)      )    );    localStorage.setItem(FAVORITES_IDS_KEY, JSON.stringify(safe));  } catch {    \/\/ Storage \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u0435\u043d \u0438\u043b\u0438 \u0434\u0430\u043d\u043d\u044b\u0435 \u043d\u0435 \u0437\u0430\u043f\u0438\u0441\u0430\u043b\u0438\u0441\u044c.  }}\u0427\u0442\u0435\u043d\u0438\u0435 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u043b\u043e\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 id. \u0417\u0430\u043f\u0438\u0441\u044c \u0443\u0434\u0430\u043b\u044f\u0435\u0442 \u0434\u0443\u0431\u043b\u0438. \u041e\u0448\u0438\u0431\u043a\u0430 JSON \u0438 \u043d\u0435\u0434\u043e\u0441\u0442\u0443\u043f\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043d\u0435 \u0440\u043e\u043d\u044f\u044e\u0442 UI.\u0412 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043d\u0435\u043b\u044c\u0437\u044f \u0441\u0447\u0438\u0442\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u043e\u0441\u0442\u043e\u0432\u0435\u0440\u043d\u044b\u043c\u0438. \u041f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c \u043c\u043e\u0436\u0435\u0442 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 DevTools. \u0421\u0442\u0430\u0440\u044b\u0439 \u043a\u043e\u0434 \u043c\u043e\u0433 \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0434\u0440\u0443\u0433\u043e\u0439 \u0444\u043e\u0440\u043c\u0430\u0442. \u0420\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430 \u0442\u043e\u0436\u0435 \u043c\u043e\u0436\u0435\u0442 \u043e\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0434\u0440\u0443\u0433\u0438\u0435 \u0434\u0430\u043d\u043d\u044b\u0435. \u041f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0438 \u043a\u0430\u0436\u0434\u043e\u0433\u043e id \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0432\u043d\u0443\u0442\u0440\u0438 storage-\u0441\u043b\u043e\u044f.\u0421\u043e\u0431\u044b\u0442\u0438\u0435 storagestorage \u0441\u0438\u043d\u0445\u0440\u043e\u043d\u0438\u0437\u0438\u0440\u0443\u0435\u0442 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u043e\u0434\u043d\u043e\u0433\u043e origin. \u0421\u043e\u0431\u044b\u0442\u0438\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442 \u0432 \u0434\u0440\u0443\u0433\u0438\u0435 \u0432\u043a\u043b\u0430\u0434\u043a\u0438 \u0438 \u043e\u043a\u043d\u0430. \u0412\u043a\u043b\u0430\u0434\u043a\u0430, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u0432\u044b\u0437\u0432\u0430\u043b\u0430 localStorage.setItem, \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u0441\u043e\u0431\u044b\u0442\u0438\u044f storage \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442. \u0418\u0437-\u0437\u0430 \u044d\u0442\u043e\u0433\u043e \u043e\u0434\u0438\u043d listener \u043d\u0430 storage \u043d\u0435 \u043e\u0431\u043d\u043e\u0432\u0438\u0442 badge \u0432 \u0442\u043e\u0439 \u0436\u0435 \u0432\u043a\u043b\u0430\u0434\u043a\u0435. \u041a\u043d\u043e\u043f\u043a\u0430 \u0437\u0430\u043f\u0438\u0448\u0435\u0442 \u043d\u043e\u0432\u044b\u0439 id, \u0430 \u0441\u0447\u0451\u0442\u0447\u0438\u043a \u0432 \u043d\u0430\u0432\u0438\u0433\u0430\u0446\u0438\u0438 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u043f\u0440\u0435\u0436\u043d\u0438\u043c \u0434\u043e \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u044f. \u041f\u0440\u043e\u0435\u043a\u0442 \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u043f\u043e\u0441\u043b\u0435 \u043a\u0430\u0436\u0434\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438.export const GOODS_STORAGE_EVENT = &#171;goods-finder:storage&#187;;function emitGoodsStorageEvent() {  if (typeof window === &#171;undefined&#187;) return;  window.dispatchEvent(new Event(GOODS_STORAGE_EVENT));}export function writeFavoriteIds(ids) {  try {    const safe = Array.from(new Set(ids));    localStorage.setItem(FAVORITES_IDS_KEY, JSON.stringify(safe));    emitGoodsStorageEvent();  } catch {    \/\/ ignore  }}\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0432 \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0432\u043a\u043b\u0430\u0434\u043a\u0435. \u0421\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u043d\u043e\u0435 storage \u043e\u0441\u0442\u0430\u0451\u0442\u0441\u044f \u0434\u043b\u044f \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u043a\u043b\u0430\u0434\u043e\u043a. \u041a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u044b \u043f\u043e\u0434\u043f\u0438\u0441\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u043e\u0431\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u044f.&#187;use client&#187;;import { useEffect, useState } from &#171;react&#187;;import {  GOODS_STORAGE_EVENT,  readFavoriteIds,} from &#171;@\/app\/_storage\/goodsStorage&#187;;export default function FavoritesBadge() {  const [count, setCount] = useState(() =&gt; readFavoriteIds().length);  useEffect(() =&gt; {    const update = () =&gt; setCount(readFavoriteIds().length);    window.addEventListener(GOODS_STORAGE_EVENT, update);    window.addEventListener(&#171;storage&#187;, update);    return () =&gt; {      window.removeEventListener(GOODS_STORAGE_EVENT, update);      window.removeEventListener(&#171;storage&#187;, update);    };  }, []);  if (!count) return null;  return &lt;span&gt;{count}&lt;\/span&gt;;}\u041e\u0434\u0438\u043d \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u043f\u0435\u0440\u0435\u0447\u0438\u0442\u044b\u0432\u0430\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u043b\u0438\u0449\u0435 \u043f\u043e\u0441\u043b\u0435 \u043e\u0431\u043e\u0438\u0445 \u0441\u043e\u0431\u044b\u0442\u0438\u0439.\u0418\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0435\u0418\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0435 \u0445\u0440\u0430\u043d\u0438\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u043c\u0430\u0441\u0441\u0438\u0432 id. \u041d\u0430\u0437\u0432\u0430\u043d\u0438\u0435, \u0446\u0435\u043d\u0430 \u0438 \u0438\u0437\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u0435 \u043e\u0441\u0442\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0439 \u0441\u0442\u043e\u0440\u043e\u043d\u0435.[7, 31, 4]\u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u0447\u0438\u0442\u0430\u0435\u0442 id \u0438\u0437 localStorage, \u0437\u0430\u0442\u0435\u043c \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0435\u0442 \u043a\u0430\u0440\u0442\u043e\u0447\u043a\u0438 \u0447\u0435\u0440\u0435\u0437 \u0432\u043d\u0443\u0442\u0440\u0435\u043d\u043d\u0438\u0439 API.async function loadFavoriteItems(ids) {  const results = await Promise.all(    ids.map(async id =&gt; {      const res = await fetch(`\/api\/goods\/${id}`, {        cache: &#171;no-store&#187;,      });      const data = await res.json().catch(() =&gt; null);      if (!res.ok || !data?.ok) {        return { ok: false, id };      }      return { ok: true, item: data.item };    })  );  return results    .filter(result =&gt; result.ok)    .map(result =&gt; result.item);}localStorage \u0445\u0440\u0430\u043d\u0438\u0442 \u0432\u044b\u0431\u043e\u0440 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f. API \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0435\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0442\u043e\u0432\u0430\u0440\u043e\u0432.\u0412 \u0441\u043f\u0438\u0441\u043a\u0435 \u0435\u0441\u0442\u044c \u0434\u0432\u0430 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f. \u041f\u0435\u0440\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 id \u0438\u0437 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0430. \u0412\u0442\u043e\u0440\u043e\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u0442 \u0437\u0430\u0433\u0440\u0443\u0436\u0435\u043d\u043d\u044b\u0435 \u043a\u0430\u0440\u0442\u043e\u0447\u043a\u0438.const [ids, setIds] = useState(() =&gt; readFavoriteIds());const [items, setItems] = useState([]);useEffect(() =&gt; {  let cancelled = false;  async function run() {    if (ids.length === 0) {      setItems([]);      return;    }    const loaded = await loadFavoriteItems(ids);    if (!cancelled) setItems(loaded);  }  run();  return () =&gt; {    cancelled = true;  };}, [ids]);\u0424\u043b\u0430\u0433 cancelled \u043d\u0435 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442 \u043e\u0442\u0432\u0435\u0442 \u0432 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442 \u043f\u043e\u0441\u043b\u0435 cleanup \u044d\u0444\u0444\u0435\u043a\u0442\u0430.\u0418\u0441\u0442\u043e\u0440\u0438\u044f \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u043e\u0432\u0418\u0441\u0442\u043e\u0440\u0438\u044f \u0445\u0440\u0430\u043d\u0438\u0442 id, \u0437\u0430\u0433\u043e\u043b\u043e\u0432\u043e\u043a \u0438 \u0432\u0440\u0435\u043c\u044f \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0430. \u0417\u0430\u043f\u0438\u0441\u044c \u0441 \u0442\u0435\u043c \u0436\u0435 id \u0443\u0434\u0430\u043b\u044f\u0435\u0442\u0441\u044f, \u0437\u0430\u0442\u0435\u043c \u043d\u043e\u0432\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u0441\u0442\u0430\u0432\u0438\u0442\u0441\u044f \u0432 \u043d\u0430\u0447\u0430\u043b\u043e. \u0414\u043b\u0438\u043d\u0430 \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0430 \u0434\u0432\u0430\u0434\u0446\u0430\u0442\u044c\u044e \u044d\u043b\u0435\u043c\u0435\u043d\u0442\u0430\u043c\u0438.export function addToHistory(id, title) {  const safeId = normalizePositiveInt(id);  if (!safeId) return;  const safeTitle = normalizeTitle(title);  const now = new Date().toISOString();  const current = readHistoryRaw();  const filtered = current.filter(item =&gt; {    if (!item || typeof item !== &#171;object&#187;) return true;    return Number(item.id) !== safeId;  });  const next = [    { id: safeId, title: safeTitle, at: now },    &#8230;filtered,  ].slice(0, HISTORY_LIMIT);  writeHistoryRaw(next);}\u041e\u0442\u043a\u0440\u044b\u0442\u0438\u0435 \u043a\u0430\u0440\u0442\u043e\u0447\u043a\u0438 \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0438\u0437 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u043e\u0433\u043e Client Component \u0431\u0435\u0437 UI.&#187;use client&#187;;import { useEffect } from &#171;react&#187;;import { addToHistory } from &#171;@\/app\/_storage\/goodsStorage&#187;;export default function HistoryPing({ id, title }) {  useEffect(() =&gt; {    addToHistory(id, title);  }, [id, title]);  return null;}\u042d\u0444\u0444\u0435\u043a\u0442 \u0441\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u0432 \u0431\u0440\u0430\u0443\u0437\u0435\u0440\u0435 \u043f\u043e\u0441\u043b\u0435 mount \u043a\u0430\u0440\u0442\u043e\u0447\u043a\u0438.\u0420\u0430\u0437\u0434\u0435\u043b\u0435\u043d\u0438\u0435 \u043a\u043e\u043c\u043f\u043e\u043d\u0435\u043d\u0442\u043e\u0432\u0421\u0442\u0440\u0430\u043d\u0438\u0446\u0430 \u0442\u043e\u0432\u0430\u0440\u0430 \u043c\u043e\u0436\u0435\u0442 \u043e\u0441\u0442\u0430\u0442\u044c\u0441\u044f \u0441\u0435\u0440\u0432\u0435\u0440\u043d\u043e\u0439. \u0414\u0430\u043d\u043d\u044b\u0435 \u0442\u043e\u0432\u0430\u0440\u0430 \u0437\u0430\u0433\u0440\u0443\u0436\u0430\u044e\u0442\u0441\u044f \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435. \u041a\u043d\u043e\u043f\u043a\u0430 \u0438\u0437\u0431\u0440\u0430\u043d\u043d\u043e\u0433\u043e \u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u0438\u0441\u0442\u043e\u0440\u0438\u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u044e\u0442\u0441\u044f \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u0438\u043c\u0438 \u043e\u0441\u0442\u0440\u043e\u0432\u043a\u0430\u043c\u0438. export default async function GoodsDetailsPage({ params }) {  const { id } = await params;  const item = await getProductById(id);  return (    &lt;article&gt;      &lt;HistoryPing id={item.id}&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-489174","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/489174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=489174"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/489174\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=489174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=489174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=489174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}