Skip to content

frontloadMeta.pending when nothing is returned #59

@matthewlein

Description

@matthewlein

Upgrading to v2 and curious about how frontloadMeta.pending works. I have noticed that when nothing is returned from the useFrontload function, there is nothing in the frontloadServerRender.data. And if I'm reading this right

const frontloadState = React.useContext(FrontloadContext)!
const serverRendered = frontloadState.isFirstRender()
const pendingInitial = serverRendered
? !frontloadState.hasFrontloadRunOnServer(key)
: true
const data = serverRendered
? frontloadState.getFrontloadServerRenderedData(key)
: undefined
const error = !!data?._____FRONTLOAD_____isServerRenderError
const [state, setState] = React.useState<{
data: T
frontloadMeta: FrontloadMeta
}>({
data,
frontloadMeta: {
serverRendered,
pending: pendingInitial,
done: !pendingInitial,
error,
},
})

it uses the keys as an indicator for whether frontload has run or not.

Here's an example of a redux thunk that doesn't need to return anything, because it is handled in the redux store.

useFrontload('ArticleListContainer', async () => {
  await dispatch(fetchAllArticles());
});

So if nothing is returned, it seems like frontloadMeta.pending is always true.

By adding a simple return true

useFrontload('ArticleListContainer', async () => {
  await dispatch(fetchAllArticles());
  return true;
});

frontloadMeta.pending works as expected.

It seems like the options are to either:

  1. Require/recommend that people return something from every useFrontload function, update the docs to include that.
  2. Return something like true in the absence of a returned value (maybe here
    this.serverRender.cache[key] = data
    )

What do you think?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions