question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What is the best way to define common functions in a context?

See original GitHub issue

I have an application that I am trying to rewrite from Nuxt to vite-plugin-ssr, but there is some difficulties.

The app uses simple authentication with access token stored in cookies. This token should be used for requests to the remote API via onBeforeRender and in components (or in an application store). While it’s easy with components and you can create an http-client (fetch) instance that is available everywhere, with onBeforeRender it’s quite difficult because this function is outside the app and it is executed both on the server and on the client (when you go from page to page).

Is there any to implement something like this using vite-plugin-ssr API?

async function onBeforeRender(pageContext: PageContext & PageContextBuiltIn) {
  const params = pageContext.routeParams;

  const [schedule, latestNews] = await Promise.all([
    // user specific schedule
    $fetch('schedule'),
    // personalised news feed
    $fetch('posts', { 
      query: {
        limit: 10,
      },
    }),
  ]);
  // ...
}

Where $fetch is a custom fetch function that was created with the app and contains default headers with authorization token.

The same question if I want access the store in onBeforeRender — is there any way to define context functions before onBeforeRender in use them in it?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
brilloutcommented, Feb 6, 2022

Yea it’s not ideal.

I believe what you want is https://github.com/brillout/vite-plugin-ssr/issues/164 which is on the roadmap.

It’s a common use case to want to control the initialization of pageContext.

You can actually already do that on the server-side (renderPage(pageContextInit)) but not on the client-side nor while pre-rendering.

0reactions
brilloutcommented, Feb 8, 2022

Closing in favor of #164.

Let me know if you believe onInit() wouldn’t be enough for you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Context Functions - Scala 3 - EPFL
As a larger example, here is a way to define constructs for checking arbitrary postconditions using an extension method ensuring so that the...
Read more >
Context Functions
As a larger example, here is a way to define constructs for checking arbitrary postconditions using an extension method ensuring so that the...
Read more >
Js Advanced Functions Context And... - Learn.co
Learning Goals. Define execution context; Define this; Access implicitly-set context in an Object-contained function expression; Access implicitly-set ...
Read more >
What is a best practice for ensuring "this" context in Javascript?
a more convenient way is to invoke Function.prototype.bind to bind a context to a function (forever). However, the only restriction here is ...
Read more >
Understanding the Invocation Context in JavaScript
In JavaScript, functions are objects which provide a modular way to define a set of procedures or operations that can be used many...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found