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

OnDemand: async extraData #560

Closed
indus opened this issue Jan 24, 2024 · 3 comments · Fixed by #561
Closed

OnDemand: async extraData #560

indus opened this issue Jan 24, 2024 · 3 comments · Fixed by #561
Labels
enhancement New feature or request

Comments

@indus
Copy link

indus commented Jan 24, 2024

Enter your suggestions in details:

To allow the on-demand generation of pages based on an async operation (like a fetch from an external source) the extraData function should allow Promises or async.

When I try this I get an internal server error:

site.use(onDemand({
  extraData(request: Request) {
    const searchParams = new URL(request.url).searchParams;
    const params = Object.fromEntries(searchParams.entries());

    return new Promise ((resolve, reject)=>{
      setTimeout(()=>{
        resolve({params})
      },1000)
    })
  },
}));

marking the function as async also gives an error.

I think an await in this line https://github1s.com/lumeland/lume/blob/main/middlewares/on_demand.ts#L42-L43
could do the trick to allow for async extraData functions.

@indus indus added the enhancement New feature or request label Jan 24, 2024
@indus indus changed the title Async OnDemand OnDemand: async extraData Jan 24, 2024
@oscarotero
Copy link
Member

That's good idea.
Do you want to work on a pull request?

@indus
Copy link
Author

indus commented Jan 25, 2024

I've just opened the PR.
I haven't found any tests for the onDemand plugin I could extend...
But I've tested it with and without extraData function async and Promises. Everything looks fine.

@oscarotero
Copy link
Member

Yep, I should add some tests to this plugin. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants