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

Blocking resource view rendered in template #1119

Closed
ogesaku opened this issue May 29, 2023 · 4 comments
Closed

Blocking resource view rendered in template #1119

ogesaku opened this issue May 29, 2023 · 4 comments
Labels

Comments

@ogesaku
Copy link

ogesaku commented May 29, 2023

I'm trying to create an SEO-friendly html response with blog post content using a create_blocking_resource.

#[component]
fn Post(cx: Scope) -> impl IntoView {
    let post = create_blocking_resource(
        cx,
        || (),
        |_| async move {
            get_post(0)
                .await
                .map(|data| data.ok_or(PostError::PostNotFound))
                .map_err(|_| PostError::ServerError)
                .flatten()
        },
    );
    let post_view = move |cx| {
        post.with(cx, |post| {
            post.clone().map(|post| {
                view! { cx,
                    <h1>{&post.title}</h1>
                    <p>{&post.content}</p>
                    <Title text=post.title/>
                    <Meta name="description" content=post.content/>
                }
            })
        })
    };

    view! { cx,
        <Suspense fallback=|| "Loading post...">
            {post_view(cx)}
        </Suspense>
    }
}

In the browser, all looks well. When I enter the blog post page, the page is rendered with blog post content (fallback does not appear), but when I check the response using curl I can see that the blog post content is not rendered in its final destination:

<!-- removed head, comments and scripts -->
<body>
  <!>Loading post...
  <template id="0-0-4-1-2-0-0f">
    <h1 id="_0-0-4-1-2-0-5">My first post</h1>
    <p id="_0-0-4-1-2-0-6">This is my first post</p>
  </template>
</body>

I would expect to get:

<body>
    <h1>My first post</h1>
    <p>This is my first post</p>
</body>
@gbj
Copy link
Collaborator

gbj commented May 29, 2023

Are you familiar with the ssr prop on a <Route/>? There is now an SsrMode::PartiallyBlocked which combines out-of-order streaming with replacing the fallback HTML for suspended fragments that use blocking resources on the server rather than in the browser. Otherwise you can use SsrMode::Async or SsrMode::InOrder and both will wait for suspended chunks on the server entirely.

@gbj gbj added the support label May 29, 2023
@ogesaku
Copy link
Author

ogesaku commented May 30, 2023

Thanks, with PartiallyBlocked it works perfectly.
Is there usage for create_blocking_resource with other ssr modes?

@gbj
Copy link
Collaborator

gbj commented Jun 2, 2023

Is there usage for create_blocking_resource with other ssr modes?

In any SSR mode, create_blocking_resource prevents the server from sending any part of the HTTP response until any suspense that has read from that resource has loaded. This is useful for doing things like setting headers or <head> tags.

@gbj gbj closed this as completed Jun 2, 2023
@g2p
Copy link
Contributor

g2p commented Sep 21, 2023

FWIW, I think there's room for improving the documentation. The way I read the book page on SSR modes, and even when looking at the SsrMode docs (I must have skimmed past the enum values), I though partially-blocking mode described the combination of SsrMode::OutOfOrder with create_blocking_resource. I think the book page should mention the flag values, and the docs for create_blocking_resource should as well. Will try to send a PR.

g2p added a commit to g2p/leptos that referenced this issue Sep 21, 2023
g2p added a commit to g2p/leptos that referenced this issue Sep 21, 2023
gbj pushed a commit that referenced this issue Sep 22, 2023
…rces (#1765)

Meant to address users making the same mistake as
#1119
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants