# Agent-js does not work in Next.js SSG

**URL:** <https://forum.dfinity.org/t/agent-js-does-not-work-in-next-js-ssg/34090>\
**Category:** Developers\
**Created:** [August 11, 2024, 4:20pm UTC](https://forum.dfinity.org/t/agent-js-does-not-work-in-next-js-ssg/34090 "2024-08-11T16:20:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [August 11, 2024, 4:20pm UTC](https://forum.dfinity.org/t/agent-js-does-not-work-in-next-js-ssg/34090/1 "2024-08-11T16:20:36Z")

</div>

Am I doing something wrong, or does the `@dfinity/agent` not work in the Next.js SSG context?

In a pre-rendered Next.js project set for static export (`output: "export"`), I am defining the request `generateStaticParams`, which is used to generate the dynamic routes at build time. Within that function, I use the `HttpClient` to fetch some data, as in this dummy example:

```javascript
export async function generateStaticParams() {
    const agent = await HttpAgent.create({
        identity: new AnonymousIdentity(),
        host: "https://icp-api.io",
        verifyQuerySignatures: true,
    });

    const actor = Actor.createActor(idlFactory, {
        agent,
        canisterId: "ryjl3-tyaaa-aaaaa-aaaba-cai"
    });

    const {icrc1_balance_of} = actor;

    // ⚠️ ERRORS
    // - Invalid signature from replica
    // - Certificate is signed more than 5 minutes in the past ...
    console.log(await icrc1_balance_of({
        owner: Principal.fromText("2vxsx-fae"),
        subaccount: []
    }))

    return [{product_id: "2vxsx-fae"}]
}

```

As a result, I end up facing the following errors (one or the other):

> Error: Invalid certificate: Invalid signature from replica ncr4b-rasb7-tueb3-n4uos-5nxou-3wbxv-xmyt3-wfdsd-vu4b6-5x3cp-aqe signed query.

or

> Error: Invalid certificate: Certificate is signed more than 5 minutes in the past. Certificate time: 2024-08-11T15:55:32.249Z Current time: 2024-08-11T16:03:35.551Z

So I’m wondering if I am doing something wrong or if indeed Agent-js does not work in that context?

This error was reported by a Juno developer, and I am able to reproduce it with a sample repo:

```auto
git clone https://github.com/peterpeterparker/next-agentjs
cd next-agentjs
npm ci
npm run dev

```

Open URL [http://localhost:3000/products/2vxsx-fae](http://localhost:3000/products/2vxsx-fae)

 ![Capture d’écran 2024-08-11 à 18.17.42](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/5/4/54da3e2261a938d4a9a464171f69b8c0b2a9e14c.jpeg)

 ![Capture d’écran 2024-08-11 à 18.10.01](https://us1.discourse-cdn.com/flex023/uploads/dfn/original/3X/b/0/b021007c92eed658c7949bdb608d1fef01c134bf.jpeg)

---

<div class="post-metadata">

**Author:** ![zohaib29](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/zohaib29/32/29625_2.png) [@zohaib29](https://forum.dfinity.org/u/zohaib29)\
**Post date:** [August 12, 2024, 9:33am UTC](https://forum.dfinity.org/t/agent-js-does-not-work-in-next-js-ssg/34090/2 "2024-08-12T09:33:11Z")

</div>

@peterparker Did you find any workarounds?

---

<div class="post-metadata">

**Author:** ![peterparker](https://avatars.discourse-cdn.com/v4/letter/p/b9bd4f/32.png) [@peterparker](https://forum.dfinity.org/u/peterparker)\
**Post date:** [August 12, 2024, 9:38am UTC](https://forum.dfinity.org/t/agent-js-does-not-work-in-next-js-ssg/34090/3 "2024-08-12T09:38:52Z")

</div>

Kind of, see this [PR](https://github.com/krlsdgzmn/agira-dapp/pull/2).

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [August 12, 2024, 6:34pm UTC](https://forum.dfinity.org/t/agent-js-does-not-work-in-next-js-ssg/34090/4 "2024-08-12T18:34:24Z")

</div>

I’d use `createSync` for SSG contexts as the simple fix, but the point is probably more to do with `fetch` or `syncTime` not working as intended.

I have another reported issue with the `syncTime` flow, so I’m going to dive into this and get back to you

---

<div class="post-metadata">

**Author:** ![BustyBeardo](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/bustybeardo/32/16506_2.png) [@BustyBeardo](https://forum.dfinity.org/u/BustyBeardo)\
**Post date:** [August 22, 2024, 1:32pm UTC](https://forum.dfinity.org/t/agent-js-does-not-work-in-next-js-ssg/34090/5 "2024-08-22T13:32:40Z")

</div>

Hi Any luck fixing this?

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [August 22, 2024, 3:51pm UTC](https://forum.dfinity.org/t/agent-js-does-not-work-in-next-js-ssg/34090/7 "2024-08-22T15:51:00Z")

</div>

I’ve been able to “solve” the syncTime issue by parsing the error response, but it reveals a fundamental design problem that there’s no secure way to bring a client into agreement with the replica time without first agreeing upon the actual time!

It will be a while before I can ship doing this automatically, but I can instead come up with a flow that allows a developer to override the time manually, and I’ll document a flow for that

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [August 22, 2024, 4:28pm UTC](https://forum.dfinity.org/t/agent-js-does-not-work-in-next-js-ssg/34090/8 "2024-08-22T16:28:33Z")

</div>

I’ll probably roll this into an interface spec proposal as well.

I think the only way to guard against a replay attack is to have an endpoint where you can request the `state_tree` using a `nonce`, and to have the `nonce` reflected back to you, so you can be sure that the replica is responding to your unique request.

---

<div class="post-metadata">

**Author:** ![anon74414410](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@anon74414410](https://forum.dfinity.org/u/anon74414410)\
**Post date:** [October 1, 2024, 4:37pm UTC](https://forum.dfinity.org/t/agent-js-does-not-work-in-next-js-ssg/34090/10 "2024-10-01T16:37:18Z")

</div>

I thought I had, but then the 2.0.0 release was bugged. Still needs more development, and a proper way to get back a certified time that we could update to. The research team is discussing how certified time could be discovered
