#Read and Write to json

3 messages · Page 1 of 1 (latest)

reef steeple
#

Before i start, im local hosting this so dont worry about vercel not allowing to write json


import Hero from "./hero";

import { getMetadata } from "@/app/meta";

import { promises as fs } from "fs";
import { getJson } from "@/data/getJson";
import { unstable_cache } from "next/cache";

export type JsonData = {
  sign: number;
  trash: number;
  hoglan: number;
  gui: number;
};
export default async function Page() {
  const data = (await unstable_cache(
    async () => {
      const file = await fs.readFile(
        process.cwd() + `/data/${"contentDownloads"}.json`,
        "utf8"
      );
      const locContent = JSON.parse(file);
      return locContent;
    },
    ["json"],
    {
      tags: ["content"],
      revalidate: 1,
    }
  )()) as JsonData;

  return (
   
        <Content serverData={data} />
     
  );
}
``` this is my page at /content,

my json file
{"sign":58,"trash":51,"hoglan":50,"gui":50}

my button
```jsx
"use client";


import { WriteNewJson } from "./writeJson";
import { useRouter } from "next/navigation";

export default function DownloadButton({
  fileName,
  downloadName,
}: {
  fileName: string;
  downloadName: string;
}) {
  const router = useRouter();
  return (
    <button
      onClick={async () => {
        await WriteNewJson({
          downloadName,
        });
        router.refresh();
        router.push(`/`);
      }}
     
    >
            <p c>DOWNLOAD</p>
    </button>
  );
}

my action

"use server";
import { getJson } from "@/data/getJson";
import { setJson } from "@/data/setJson";
import { revalidatePath, revalidateTag } from "next/cache";

export async function WriteNewJson({ downloadName }: { downloadName: string }) {
  const prevJson = await getJson("contentDownloads");
  prevJson[downloadName] += 1;
  const newJson = JSON.stringify(prevJson);
  await setJson("contentDownloads", newJson);
  revalidatePath("/content");
}

Why is the page not updating to show the new data from the json

viscid cradleBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

reef steeple
#

ok router.push was the issue