analog icon indicating copy to clipboard operation
analog copied to clipboard

SSR load executed twice: once on server, once on client after build

Open leblancmeneses opened this issue 4 months ago • 0 comments

Please provide the environment you discovered this bug in.

node: 20.11.1, Apple M1 Max, analogjs 1.8.2

Which area/package is the issue in?

vite-plugin-nitro

Description

Project Setup

scaffold with option: "Full-stack Application"

npm create analog@latest
cd analog-project

index.page.ts

import { Component } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';

import { injectLoad } from '@analogjs/router';
import { load } from './index.server'; // not included in client build

@Component({
  selector: 'app-home',
  standalone: true,
  template: `
    <h2>Hello: {{data().hello}}</h2>
  `,
})
export default class HomeComponent {
  data = toSignal(injectLoad<typeof load>(), { requireSync: true });
}

index.server.ts

import { PageServerLoad } from '@analogjs/router';

export const load = async (pageServerLoad: PageServerLoad) => {
  await new Promise((resolve) => setTimeout(resolve, 5000));

  return {
    hello: 'world',
  };
};

Please provide the exception or error you saw

npm run dev


1. xhr request to load is *not* visible in chrome dev tools. (correctly ssr'ed)


```bash
npm run build
cd dist/analog
node server/index.mjs
  1. xhr request to load is visible in chrome dev tools. was pre-rendered with default vite.config.ts. Then, when the page completed on client, it requested "load" again.
# updating vite.config.ts to https://github.com/analogjs/analog/pull/1379/files
# so site is 100% ssr'ed
npm run build
cd dist/analog
node server/index.mjs
  1. xhr request to load is visible in chrome dev tools. was ssr'ed meaning server executed load, then the client refreshed by calling load again on the client.


### Other information

example:
https://www.improvingstartups.com/startupwiki/
home page is ssr'ed and xhr request to `/api/_analog/pages/-index-`  is unexpected .

### I would be willing to submit a PR to fix this issue

- [ ] Yes
- [X] No

leblancmeneses avatar Oct 03 '24 14:10 leblancmeneses