analogjs / analog

The fullstack meta-framework for Angular. Powered by Vite and Nitro
https://analogjs.org
MIT License
2.59k stars 250 forks source link

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

Open leblancmeneses opened 1 month ago

leblancmeneses commented 1 month ago

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