# Running Custom Javascript SSR doesn't seem to work

**URL:** <https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81>\
**Category:** Technical Questions\
**Tags:** custom-code\
**Created:** [May 11, 2020, 12:21pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81 "2020-05-11T12:21:21Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ismailp](https://avatars.discourse-cdn.com/v4/letter/i/87869e/32.png) [@ismailp](https://forum.builder.io/u/ismailp)\
**Post date:** [May 11, 2020, 12:21pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/1 "2020-05-11T12:21:21Z")

</div>

I’m trying to run some custom JS from within my symbol in SSR mode but can’t seem to get it to work.

I have an input field named `product` with the type `map`. I’m logging my api and it doesn’t seem to hit the endpoint at all and the state has no data.

Am I doing this correctly?

My code:

```auto
async function main() {
    if (Builder.isServer) {
        await fetch('/api/product?sku=MY_SKU')
        .then(res => res.json())
        .then(data => {
            state.product = data;
        });
    }
}

export default main();

```

---

<div class="post-metadata">

**Author:** ![korey](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.builder.io/korey/32/12_2.png) [@korey](https://forum.builder.io/u/korey)\
**Post date:** [May 11, 2020, 4:13pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/2 "2020-05-11T16:13:08Z")

</div>

Hi, @ismailp. Looking at your code, overall I think you are doing it correctly. One thing I noticed though is in the fetch you are using a relative url. The url needs to include the domain of your API as well so Builder can make the fetch call. Can you try `await fetch('https://www.thedomainofyourapi.com/api/product?sku=MY_SKU')`?

---

<div class="post-metadata">

**Author:** ![ismailp](https://avatars.discourse-cdn.com/v4/letter/i/87869e/32.png) [@ismailp](https://forum.builder.io/u/ismailp)\
**Post date:** [May 11, 2020, 6:00pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/3 "2020-05-11T18:00:55Z")

</div>

@korey ok got it, It’s pointing to `localhost`. Would that work?

Thanks!

---

<div class="post-metadata">

**Author:** ![korey](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.builder.io/korey/32/12_2.png) [@korey](https://forum.builder.io/u/korey)\
**Post date:** [May 11, 2020, 6:15pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/4 "2020-05-11T18:15:52Z")

</div>

That will not work, because `localhost` is not accessible on the public internet by the Builder servers. For now, you probably want to remove the `Builder.isServer` code and let the fetch happen client side, where the code will have access to your local server. Then, when you have deployed your API code, you can add back in the `isServer` check if you want to.

---

<div class="post-metadata">

**Author:** ![ismailp](https://avatars.discourse-cdn.com/v4/letter/i/87869e/32.png) [@ismailp](https://forum.builder.io/u/ismailp)\
**Post date:** [May 11, 2020, 7:17pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/5 "2020-05-11T19:17:45Z")

</div>

OK Cool,

Will try it out. Appreciate the help!

Best,  
Ismail

---

<div class="post-metadata">

**Author:** ![ismailp](https://avatars.discourse-cdn.com/v4/letter/i/87869e/32.png) [@ismailp](https://forum.builder.io/u/ismailp)\
**Post date:** [May 11, 2020, 7:32pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/6 "2020-05-11T19:32:15Z")

</div>

@korey I set an absolute URL but still get no hits to the API when doing SSR. Works on the client though.

Here’s the url to the symbol: [https://builder.io/content/bb318e2eb7b34ec2876744c1137afe1a](https://builder.io/content/bb318e2eb7b34ec2876744c1137afe1a)

Also you preview it the SSR version here: [https://hs.mersenne.now.sh/test-ismail](https://hs.mersenne.now.sh/test-ismail) and the client side version here: [https://hs.mersenne.now.sh/b/test-ismail](https://hs.mersenne.now.sh/b/test-ismail)

Code I’m running now is:

```
async function main() {
    await fetch('https://hs-git-product-api-endpoint.mersenne.now.sh/api/product-widget?sku=LEATHER_PURSE-BLACK_LANDSCAPE')
        .then(res => res.json())
        .then(data => {
            console.log(data);
            // set the data on the state object so you can access it from your components
            state.product = data;
        });
}

export default main();
```

---

<div class="post-metadata">

**Author:** ![ismailp](https://avatars.discourse-cdn.com/v4/letter/i/87869e/32.png) [@ismailp](https://forum.builder.io/u/ismailp)\
**Post date:** [May 12, 2020, 4:47pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/7 "2020-05-12T16:47:23Z")

</div>

Any updates on this @korey?

Thanks!

---

<div class="post-metadata">

**Author:** ![korey](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.builder.io/korey/32/12_2.png) [@korey](https://forum.builder.io/u/korey)\
**Post date:** [May 12, 2020, 6:08pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/8 "2020-05-12T18:08:34Z")

</div>

@ismailp, I just took a closer look and I am not sure exactly why the code is not setting state on the server. However, I think it might be a bug that will take some more time to investigate. Hopefully you can use the client side solution for now, and I will let you know when we fix the issue.

---

<div class="post-metadata">

**Author:** ![ismailp](https://avatars.discourse-cdn.com/v4/letter/i/87869e/32.png) [@ismailp](https://forum.builder.io/u/ismailp)\
**Post date:** [May 12, 2020, 6:30pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/9 "2020-05-12T18:30:44Z")

</div>

@korey unfortunately we are running the AMP model and can’t run any client side JavaScript on our builder pages.

@steve8709 any way you guys could prioritize this?

Thanks!

---

<div class="post-metadata">

**Author:** ![korey](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.builder.io/korey/32/12_2.png) [@korey](https://forum.builder.io/u/korey)\
**Post date:** [May 12, 2020, 7:02pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/10 "2020-05-12T19:02:40Z")

</div>

@ismailp that makes sense. Sorry if my last post was not clear: we are currently investigating, so as far as priority goes it is prioritized 😄

---

<div class="post-metadata">

**Author:** ![ismailp](https://avatars.discourse-cdn.com/v4/letter/i/87869e/32.png) [@ismailp](https://forum.builder.io/u/ismailp)\
**Post date:** [May 12, 2020, 7:14pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/11 "2020-05-12T19:14:10Z")

</div>

@korey awesome! 🙂

---

<div class="post-metadata">

**Author:** ![korey](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.builder.io/korey/32/12_2.png) [@korey](https://forum.builder.io/u/korey)\
**Post date:** [May 13, 2020, 5:02pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/12 "2020-05-13T17:02:38Z")

</div>

@ismailp, looks like we have been able to pinpoint the issue. We hope to be able to deploy the fix tomorrow, and we will let you know when it is live.

---

<div class="post-metadata">

**Author:** ![ismailp](https://avatars.discourse-cdn.com/v4/letter/i/87869e/32.png) [@ismailp](https://forum.builder.io/u/ismailp)\
**Post date:** [May 13, 2020, 7:00pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/13 "2020-05-13T19:00:23Z")

</div>

@korey you guys are the best!

---

<div class="post-metadata">

**Author:** ![aziz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.builder.io/aziz/32/2551_2.png) [@aziz](https://forum.builder.io/u/aziz)\
**Post date:** [May 14, 2020, 12:55am UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/14 "2020-05-14T00:55:17Z")

</div>

We released a fix earlier for the API side, I’m still seeing an issue where only when you’re editing the ssr content is not hydrated properly, I’m investigating this, but otherwise live content should be fixed.

---

<div class="post-metadata">

**Author:** ![ismailp](https://avatars.discourse-cdn.com/v4/letter/i/87869e/32.png) [@ismailp](https://forum.builder.io/u/ismailp)\
**Post date:** [May 14, 2020, 7:12am UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/15 "2020-05-14T07:12:29Z")

</div>

@aziz ok will test it out and see!

Best,  
Ismail

---

<div class="post-metadata">

**Author:** ![ismailp](https://avatars.discourse-cdn.com/v4/letter/i/87869e/32.png) [@ismailp](https://forum.builder.io/u/ismailp)\
**Post date:** [July 17, 2020, 11:38am UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/16 "2020-07-17T11:38:44Z")

</div>

Hi again!

Running async functions i the custom JS still doesn’t run properly on SSR @aziz.

Could have a look and fix please?!

---

<div class="post-metadata">

**Author:** ![aziz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.builder.io/aziz/32/2551_2.png) [@aziz](https://forum.builder.io/u/aziz)\
**Post date:** [July 20, 2020, 4:21pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/17 "2020-07-20T16:21:08Z")

</div>

Hi @ismailp , please point me to a content link with reproduction and I’ll take a look.

---

<div class="post-metadata">

**Author:** ![ismailp](https://avatars.discourse-cdn.com/v4/letter/i/87869e/32.png) [@ismailp](https://forum.builder.io/u/ismailp)\
**Post date:** [July 21, 2020, 5:45pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/18 "2020-07-21T17:45:18Z")

</div>

Hi there @aziz. Here you go: [https://builder.io/content/350f24762e8d48c3adea91b5e753d8be](https://builder.io/content/350f24762e8d48c3adea91b5e753d8be)

---

<div class="post-metadata">

**Author:** ![aziz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.builder.io/aziz/32/2551_2.png) [@aziz](https://forum.builder.io/u/aziz)\
**Post date:** [July 22, 2020, 4:26pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/19 "2020-07-22T16:26:06Z")

</div>

The content you linked seem to be working fine to me, please let me know what’s it that’s not working from the Builder side.

---

<div class="post-metadata">

**Author:** ![ismailp](https://avatars.discourse-cdn.com/v4/letter/i/87869e/32.png) [@ismailp](https://forum.builder.io/u/ismailp)\
**Post date:** [July 22, 2020, 5:07pm UTC](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81/20 "2020-07-22T17:07:30Z")

</div>

It’s not working. I have a custom function that updates the state. This function should be run on the server side. It’s not so this is still an issue @aziz

[Next page](https://forum.builder.io/t/running-custom-javascript-ssr-doesnt-seem-to-work/81.md?page=2)
