Next.js Discord

Discord Forum

why is the time dynamic in this code?

Unanswered
NuclearMonkey posted this in #help-forum
Open in Discord
I saw this code on youtube and was curious why is the time dynamically rendered in the production build the time should be stuck in time if its in production build.
Here is the video: The code I'm pointing is at 0:39
https://youtu.be/KzS_AG6nWdg?si=TSyz5fMXWoj4HckO&t=39

110 Replies

Toyger
at 0:34 in page they have
export const dynamic = 'force-dynamic'

which is
'force-dynamic': Force dynamic rendering, which will result in routes being rendered for each user at request time. This option is equivalent to getServerSideProps() in the pages directory.
so page always dynamically rendered, and not cached.
@Toyger i think he removed it on the timestamp I gave
@NuclearMonkey <@536484914221285376> i think he removed it on the timestamp I gave
Toyger
oh, I misunderstood what he shows at first.
he have revalidateTag, so it opt-out page from fullroutecache, so date regenerates.
Opting out of the Data Cache: If a route has a fetch request that is not cached, this will opt the route out of the Full Route Cache. The data for the specific fetch request will be fetched for every incoming request. Other fetch requests that do not opt out of caching will still be cached in the Data Cache. This allows for a hybrid of cached and uncached data.
so just putting revalidateTag on the page makes it dynamic?
@NuclearMonkey so just putting revalidateTag on the page makes it dynamic?
he is trying to show how to use revalidateTag to revalidate a static page at 0:39
@NuclearMonkey so just putting revalidateTag on the page makes it dynamic?
Toyger
it opt-out it from fullroutecache on this request, so page update it cache
@Ray he is trying to show how to use revalidateTag to revalidate a static page at 0:39
i agree with OP's question, shouldnt the date get cached?
the dog pics do, which is expected behaviour.. but the vercel url means this is a production site hence i would assume the time gets cached as well especially since the dynamic export was removed
@Toyger it opt-out it from fullroutecache on this request, so page update it cache
also this is wrong, it makes the certain fetch opt out of cache, not the entire page. that being the whole point of opting out so certain parts can get cached like the time, while the fetching can opt out
is the weird behaviors written in the docs?
@Toyger but it what it says in docs
data for the specific fetch request will be fetched for every incoming request
@NuclearMonkey is the weird behaviors written in the docs?
are we sure that the vid is in production? since on dev mode its expected
the url being a vercel url is making me confused since on production ideally it gets cached
that's what I thought too. maybe the uploader made a mistake
Wait for ray since i might be missing something as well, but this isnt normal on production
@Arinji i agree with OP's question, shouldnt the date get cached?
you need to ask he @Siberian Flycatcher
oh wow he is in the server, but yea Alex, is the video in production or dev mode?
the date should be cached since there is no route segment config at 0:39
@Ray you need to ask he <@707695420721201153>
also the docs mention "this will opt the route of the full route cache" this seems to be a docs issue right? Since the fetch gets out of cache not the route
im assuming route means page here
also when I press the revalidate button, I copied the in the tutorial. my 2 images changes instead of one
in the video when pressing the button only one changes
show your code please
import { revalidateTag } from 'next/cache';

export default async function DogsPage() {
  async function revalidate(formData: FormData) {
    'use server';
    const tag = formData.get('tag') as string;
    console.log(tag);
    revalidateTag(tag);
  }

  const dog1 = await getDog1('dog1');
  const dog2 = await getDog2('dog2');

  return (
    <div>
      <h1>Dogs Page</h1>
      <h2>{Date.now()}</h2>
      <img src={dog1.message} alt="" />
      <form action={revalidate}>
        <button name="tag" value="dog1">
          Revalidate
        </button>
      </form>
      <img src={dog2.message} alt="" />
      <form action={revalidate}>
        <button name="tag" value="dog2">
          Revalidate
        </button>
      </form>
    </div>
  );
}

async function getDog1(tag: string) {
  const response = await fetch('https://dog.ceo/api/breeds/image/random', {
    next: {
      tags: ['dog1'],
    },
  });
  const data = await response.json();
  return data;
}

async function getDog2(tag: string) {
  const response = await fetch('https://dog.ceo/api/breeds/image/random', {
    next: {
      tags: ['dog2'],
    },
  });
  const data = await response.json();
  return data;
}
@Arinji in his video, only one dog photo changes when clicking a button, on my mine it 2 photo changes
when you log tag, what do you see?
dev
dev has different cache then prod
dev kinda has a hybrid cache where it tries to get the most recent data since in development thats required
on prod it changes to the more stable force cache
takes a while to get a hang of
try testing on prod
@Arinji when you log tag, what do you see?
can you confirm if it shows the 2 different tags btw cause im not that good with forms
its still the same on prod and dev
when you log the tag, what does it say?
it logs the right that when I pressed the button
@Arinji did you saw the last part of the youtube? where only one photo changes when one button is clicked?
can you help me replicate what the video did? I'm sorry for bothering you
I'm just really curious why this happens
@Arinji <@743561772069421169>
clicking either buttons updates both dog pics,
tags show correctly
same behaviour on prod
@NuclearMonkey I'm just really curious why this happens
a thing that would help is url logging, one sec
try this out and tell me what does the console show when you try and fetch
is it the same on your local machine?
i can setup a codesandbox actually
const nextConfig = {
  logging: {
    fetches: {
      fullUrl: true,
    },
  },
};
i tried this
nothing is logging
does this work on prod?
its logging now, it only works on dev
its a dev only thing
ok fixed
@NuclearMonkey
focus on the fetches
so basically the issue which i missed was you didnt cache the fetches
sure you added a tag, correct. But since you didnt specify caching behaviour.. it keps updating the images
wait one sec
weird it still seems to not be caching
shouldnt only one of the images be updating?
is alex video supposed to be the right one?
or somethings wrong with his example?
or am I missing something here
I'm really confused
even im confused atp... i dont see anything wrong with the code :/
https://wtnw2x-4000.csb.app/

Build also seems to be fucked
alfon might be able to figure something out, or ray
@NuclearMonkey check now
i changed the api to one be a cat one and the other be a dogs one
Looks like 2 get functions to the same url fucked nextjs cache detection
So alex example was not quite right?
@NuclearMonkey So alex example was not quite right?
its fine, even i didnt know nextjs did stuff like this, imma do a bit more research on this and get back to you
the time stuff is weird which we need a response from him
thanks for the help
@NuclearMonkey I saw this code on youtube and was curious why is the time dynamically rendered in the production build the time should be stuck in time if its in production build. Here is the video: The code I'm pointing is at 0:39 https://youtu.be/KzS_AG6nWdg?si=TSyz5fMXWoj4HckO&t=39
its because the page is set as force-dynamic therefore the page is rendered dynamically at request time and not at build time.

At build time, it only renders static pages such are those pages that doesn't have `force-dynamic' or any dynamic features that would convert the pages from SSG to SSR
oh the "force-dynamic" is removed
in this case, its this @NuclearMonkey
@Ray @Arinji @Toyger
The page is static therefore Date.now() is only called whenever the page rerenders.
since the page is static therefore it doesn't have any dynamic code
Any cache or data cache that are invalidated will trigger page render.
"dog" is inside the page therefore when "dog" is invalidated, the router cache will be purged and the page is invalidated (see image)
"dog" is part of the Data Cache, and is invalidated, therefore new "dog" will be fetched.
"cat" is part of the Data Cache, not Router Cache. Therefore is still the same when "dog" is invalidated.
therefore on page rerender, dog is presented with new image whereas cat stays the same (since the Data Cache is not invalidated)
and Date.now() changes since the whole page not only the dog nor cat
.
Data Cache and Router Cache are 2 different thing but any invalidation of Data Cache inside of any route will also in turn invalidate the Router Cache of the page where the Data Cache resides.

I think this is an automatic behavior that are opinionated made by Next.js since technically Data Cache can act on its own and Router Cache can act on its own but made it easier so you dont have to invalidate both Data Cache and the Router Cache
quick glossary:
Data Cache: the cache that came from the result of a function, i.e fetch() and any function wrapped by unstable_cache()

Router Cache: the cache that came from the result of a page render from a statically rendered route. This is denoted as SSG route in the build log.

Statically Rendered Route: A route that lacks any "dynamic-opting-features" (features that opts into dynamic rendering) which defaults back to static rendering.
Yea, the date keeps updating regardless of the revalidate or not
@Ray but in the video at 0:47, he refresh the page twice before pressing the revalidate button and the date also changed. the date shouldn't change at the beginning because the page should be static
Now thats weird, maybe the force-revalidate is hidden and not removed? Because the behavvior is the same in dynamic rendering
The dog cat issue is on discussions now,
https://discord.com/channels/752553802359505017/752647196419031042/1223523452141441135

the date can be accounted to dev mode.. we can assume that the url is just for show on the vid.

So I think this ticket can be marked? Or do we make the pr and wait for a response from the nextjs contributors
Since dev mode would lead to the date updating
I thought ur discussion has a different issue lol
I think the issue is the ambiguoty of the video so im with rau
Ray
@ᴉuɐpɹɐɐ I thought ur discussion has a different issue lol
Nope, it's just this forum that I cleaned up and recreated lol
But ur message in discussion presents a different issue
@ᴉuɐpɹɐɐ But ur message in discussion presents a different issue
It's ignoring the date stuff cause that's what Alex is gonna confirm
I don't think Alex is gonna respond here lol
@Arinji This the discussions one
sound like a bug👀
@Ray sound like a bug👀
:)
@Arinji :)
just tested, the cache is not creating new fetch-cache with the same url but different tag
next@14.1.4
Yay new bug found