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

onCleanup not always called on the server #1599

Closed
marbemac opened this issue Mar 7, 2023 · 2 comments
Closed

onCleanup not always called on the server #1599

marbemac opened this issue Mar 7, 2023 · 2 comments
Labels
bug Something isn't working

Comments

@marbemac
Copy link

marbemac commented Mar 7, 2023

Describe the bug

When a resource is created outside of the <Suspense> boundary that wraps its data access, the first such resource will not have related onCleanup functions triggered on the server (only the first resource created this way).

Kind of nuanced, and there seems to be some other strange but related behavior at play here as well. Easier to show with some examples.

Here's our resource helper - it just creates and returns a new resource, and registers an onCleanup function:

https://github.com/marbemac/solid-oncleanup-ssr-bug/blob/ef6a572b581791ccabaf6439061b70d6e0c59e91/src/routes/index.tsx#L25-L39

A DataComp component that does not include it's own suspense boundary:

https://github.com/marbemac/solid-oncleanup-ssr-bug/blob/ef6a572b581791ccabaf6439061b70d6e0c59e91/src/routes/index.tsx#L41-L51

A DataCompWithInnerSuspense component that DOES wrap access to the resource data in its own suspense boundary:

https://github.com/marbemac/solid-oncleanup-ssr-bug/blob/ef6a572b581791ccabaf6439061b70d6e0c59e91/src/routes/index.tsx#L53-L67

Scenarios

Render a single DataCompWithInnerSuspense

<DataCompWithInnerSuspense name="DataCompWithInnerSuspense-1" />

Resulting server logs

Note no onCleanup.

call:DataCompWithInnerSuspense-1
call:DataCompWithInnerSuspense-1.resource

Render more than one DataCompWithInnerSuspense

<DataCompWithInnerSuspense name="DataCompWithInnerSuspense-1" />
<DataCompWithInnerSuspense name="DataCompWithInnerSuspense-2" />

Resulting server logs

Note the second comp did have it's onCleanup triggered.

call:DataCompWithInnerSuspense-1
call:DataCompWithInnerSuspense-1.resource
call:DataCompWithInnerSuspense-2
call:DataCompWithInnerSuspense-2.resource
onCleanup:DataCompWithInnerSuspense-2.resource
onCleanup:DataCompWithInnerSuspense-2

Render DataComp + one or more DataCompWithInnerSuspense

<Suspense>
  <DataComp name="DataComp-1" />
</Suspense>

<DataCompWithInnerSuspense name="DataCompWithInnerSuspense-1" />

Resulting server logs

In this case onCleanup IS called for DataCompWithInnerSuspense-1.

Note however that there might be some other weirdness, not sure why the the DataComp logs are triggered again at the end (last 4 lines).

call:DataComp-1
call:DataComp-1.resource
call:DataCompWithInnerSuspense-1
call:DataCompWithInnerSuspense-1.resource
onCleanup:DataComp-1.resource
onCleanup:DataComp-1
onCleanup:DataCompWithInnerSuspense-1.resource
onCleanup:DataCompWithInnerSuspense-1
call:DataComp-1
call:DataComp-1.resource
onCleanup:DataComp-1.resource
onCleanup:DataComp-1

Your Example Website or App

https://github.com/marbemac/solid-oncleanup-ssr-bug

Steps to Reproduce the Bug or Issue

  1. Clone https://github.com/marbemac/solid-oncleanup-ssr-bug.
  2. pnpm install
  3. pnpm dev
  4. Load http://127.0.0.1:3001
  5. Adjust the components rendered to mess around with the various scenarios described in this issue (https://github.com/marbemac/solid-oncleanup-ssr-bug/blob/main/src/routes/index.tsx#L11-L18)
  6. Reload the page, view server-side logs in terminal

Expected behavior

onCleanup is called consistently.

Screenshots or Videos

No response

Platform

  • OS: na
  • Browser: na
  • Version: 1.6.12

Additional context

Originally ran into this behavior while trying to debug an issue in @tanstack/solid-query. onCleanup is used there to unsubscribe various observables, and the fact that it is not called consistently when on the server is causing problems.

@ryansolid ryansolid added the bug Something isn't working label Mar 8, 2023
@marbemac
Copy link
Author

marbemac commented Mar 9, 2023

Thanks @ryansolid! Just verified, looks great re the original issue.

One bit of potentially strange behavior persists when the resource is created within the suspense boundary - for example:

<Suspense>
  <DataComp name="DataComp-1" />
</Suspense>

Results in these logs on the server:

call:DataComp-1
call:DataComp-1.resource
onCleanup:DataComp-1.resource
onCleanup:DataComp-1
call:DataComp-1
call:DataComp-1.resource
onCleanup:DataComp-1.resource
onCleanup:DataComp-1

Is it expected for everything to be called twice?

If this is indeed unexpected and you'd prefer I open a separate issue just LMK!

@ryansolid
Copy link
Member

ryansolid commented Mar 9, 2023

It is expected. On the server we execute more like React. There is no real reactivity. So Suspense boundaries will re-run. It's just so much faster to concat strings than it is to have creative primitives in memory for this case. That being said we do remember that it was created so we don't fetch again.

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

No branches or pull requests

2 participants