Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
leblancmeneses opened this issue Oct 3, 2024 · 5 comments · Fixed by #1396
Open

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

leblancmeneses opened this issue Oct 3, 2024 · 5 comments · Fixed by #1396
Labels
bug Something isn't working

Comments

@leblancmeneses
Copy link
Contributor

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
@brandonroberts
Copy link
Member

@leblancmeneses any additional findings here?

@leblancmeneses
Copy link
Contributor Author

@brandonroberts, I haven't looked yet. The earliest I can look at this is after December ~3rd. Could you give me till the 6th to get back to you?

@leblancmeneses
Copy link
Contributor Author

@brandonroberts I looked at the project today because I started another analogjs project 🎉🎉🎉.

Here is a demo setup of the problem.
leblancmeneses/analog-bug-1383#1

It does look like the project regressed because on #1396 it used to work for prod build without basehref, however, now it does not.

I added some log statements in the area #1396 edited and noticed that on client const baseUrl = injectBaseURL();
baseUrl is null while import.meta.env.BASE_URL shows the correct value inside requestContextInterceptor.

It might be a good time to start those YouTube videos : ].

@brandonroberts
Copy link
Member

brandonroberts commented Dec 7, 2024

I took a look at the demo setup and it works correctly if you omit the VITE_ANALOG_PUBLIC_BASE_URL

pnpm run build --baseHref='/basehref/'
NITRO_APP_BASE_URL="/basehref/" node dist/analog/server/index.mjs

And include the / route in the prerender routes array.

Navigate to http://localhost:3000/basehref/ and no HttpClient request is made initially.

@leblancmeneses
Copy link
Contributor Author

leblancmeneses commented Dec 23, 2024

@brandonroberts Thank you for looking into this! Unfortunately, the current provided solution doesn't address the issue in this case. Using the documented approach for setting the baseHref and not prerendering '/' results in two API requests being made, which is not the desired behavior.

The intent of the repo was to showcase the issue for SSR when prerender is not an option e.g. homepage showing fresh data.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
@brandonroberts @leblancmeneses and others