podium-lib / issues

All podium issues, bugs, questions etc goes here. Documentation is to be found at https://podium-lib.io
1 stars 0 forks source link

Async routes for podlet.content not working #41

Closed rajasegar closed 6 months ago

rajasegar commented 3 years ago

When I do some data fetching inside podlet's content path with async await, that particular podlet content is not available to the layout.

Podlet

app.get(podlet.content(),  async (req, res) => {
  const response = await fetch('https://swapi.dev/api/planets');
  const data = await response.json();
  const planets = data.results.map(p => p.name).join('\n');
    res.status(200).podiumSend(`
        <div>
          <h1>Planets page</h1>
          ${planets}
        </div>
    `);
});

Layout

app.get('/planets', async (req, res) => {
  const incoming = res.locals.podium;
  const [ navbar, planets ] = await Promise.all([
    navpod.fetch(incoming),
    planetsPod.fetch(incoming)
  ]);

  incoming.view.title = 'Starwars - Planets';

  incoming.css = navbar.css;
  res.podiumSend(`<div>
    ${navbar}
    ${planets}
    </div>`);
});
digitalsadhu commented 3 years ago

If you log out your nabber and planets objects, what do you see?

Do you have a full running code example? It all looks fine to me at a glance but happy to run something and try it out.

rajasegar commented 3 years ago

@digitalsadhu All the code is hosted here, though I have removed the code that is not working, you can just copy / paste. https://github.com/podium-starwars You can see the demo here https://podium-starwars.herokuapp.com/

digitalsadhu commented 3 years ago

Sorry for the slow reply, I just realised what's likely happening here. When your podlet is slow to respond, the layout will abort the fetch and render the fallback instead. The default timeout for this is 1000ms. This can be configured in the layout client. See: https://podium-lib.io/docs/api/layout#client

rajasegar commented 3 years ago

Thank you @digitalsadhu will try that config.