If I put a console.log inside the code section of an Astro page, it will log twice every time I request the page. This has the implication that when I use a path parameter to make a database call, one of those two calls is valid and the other ends up with an undefined value along the way. How am I supposed to correctly run code once when a page is generated/requested?
#SSR page runs code twice
8 messages · Page 1 of 1 (latest)
Hi @distant slate. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
Astro runs on the server and never in the browser. Therefore, a console.log() statement in Astro frontmatter will always output to the terminal running the Astro CLI. If you're seeing the log twice, it might be due to the way your code is structured or due to some other part of your application triggering the log.
If you're using a path parameter to make a database call, you should ensure that this call is made within a function that is only executed once during the page generation or request. If you're using server-side rendering (SSR), you should be aware that certain features are only available at the page level and can't be used inside of components, including layout components. This is because these features modify the Response headers, which can't be modified after they're sent to the browser.
If you're using the <ViewTransitions /> component for page navigation, scripts are run in sequential order to match browser behavior. If you have code that sets up global state, this state will need to take into account that the script might execute more than once. You should check for the global state in your <script> tag, and conditionally execute your code where possible.
For example:
<script is:inline> if(!window.SomeGlobal) { window.SomeGlobal = {} // .... }</script>
You can also listen to the astro:page-load event that fires at the end of page navigation, after the new page is visible to the user and blocking styles and scripts are loaded. This event can be used to run code on every page navigation, or only once ever:
<script> document.addEventListener('astro:page-load', () => { // This only runs once. setupStuff(); }, { once: true });</script>
If you're still experiencing issues, it might be a good idea to check if there are any known bugs related to your problem in the Astro GitHub issues (https://github.com/withastro/astro/issues).
Sources:
- Debugging with console.log() (https://docs.astro.build/en/guides/troubleshooting/)
- Script behavior during page navigation (https://docs.astro.build/en/guides/view-transitions/)
- Astro.request.method (https://docs.astro.build/en/guides/server-side-rendering/)
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
@distant slate kapa.ai is still learning and improving, please let us know how it did by reacting below
@jade marsh How do I ensure the code is only run once during page generation?
Let me check the knowledge base for you. One moment! :books:
Astro provides a way to ensure that certain code is only run once during page generation. This can be achieved by using the astro:page-load event. This event fires at the end of page navigation, after the new page is visible to the user and blocking styles and scripts are loaded.
You can listen to this event on the document and use the { once: true } option to ensure that your code only runs once:
<script>
document.addEventListener('astro:page-load', () => {
// This only runs once.
setupStuff();
}, { once: true });
</script>
In this example, setupStuff() is a placeholder for the code you want to run. Replace it with your actual code.
Remember, this event is fired both on initial page navigation (MPA) and any subsequent navigation, either forwards or backwards. So, this is a good place to put code that you want to run only once during the page generation.