You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there a way to tell if a form action is invoked as part of a request from an enhanced form?
In my app I populate event.locals with a database object called "currentOrg". I have a form action that switches the current org. As per the docs, I'm updating event.locals in that form action, however depending on how the form action is being invoked this is not always necessary.
Scenario 1: basic (no enhanced form) POST
The order of ops seems to be:
hooks.server.js - handle() - sets event.locals.currentOrg based on cookie
form action invoked and sets event.locals.currentOrg to the new org + sets the cookie to contain the new org
+page.server.js - load()
+page.svelte rendered
-- done --
In this scenario, we definitely need the event.locals.currentOrg to be updated since the page's load function is being called afterward in order to render the page.
Scenario 2: enhanced form POST
hooks.server.js - handle() - sets event.locals.currentOrg based on cookie
form action invoked and response is returned as JSON to the browser (along with cookie containing new org)
-- done --
a NEW request is made to the server:
hooks.server.js - handle()
+page.server.js - load()
+page.svelte rendered
-- done --
In this scenario, there is no need to set event.locals.currentOrg in the form action since the request is completed and a brand new request is made to load the page.
Which begs the question - is there a way for a form action to know if it's being requested via an enhanced form? If there is, it would save some cycles to not have to look up the new org from the DB just to put it into event.locals.currentOrg right before the request is completed.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
Is there a way to tell if a form action is invoked as part of a request from an enhanced form?
In my app I populate
event.locals
with a database object called "currentOrg". I have a form action that switches the current org. As per the docs, I'm updatingevent.locals
in that form action, however depending on how the form action is being invoked this is not always necessary.Scenario 1: basic (no enhanced form) POST
The order of ops seems to be:
handle()
- setsevent.locals.currentOrg
based on cookieevent.locals.currentOrg
to the new org + sets the cookie to contain the new orgload()
In this scenario, we definitely need the
event.locals.currentOrg
to be updated since the page's load function is being called afterward in order to render the page.Scenario 2: enhanced form POST
handle()
- setsevent.locals.currentOrg
based on cookiehandle()
load()
In this scenario, there is no need to set
event.locals.currentOrg
in the form action since the request is completed and a brand new request is made to load the page.Which begs the question - is there a way for a form action to know if it's being requested via an enhanced form? If there is, it would save some cycles to not have to look up the new org from the DB just to put it into
event.locals.currentOrg
right before the request is completed.Beta Was this translation helpful? Give feedback.
All reactions