feat: getActionPath()#10353
Conversation
✅ Deploy Preview for astro-docs-2 ready!
To edit notification comments on pull requests, go to your Netlify site configuration. |
Lunaria Status Overview🌕 This pull request will trigger status changes. Learn moreBy default, every PR changing files present in the Lunaria configuration's You can change this by adding one of the keywords present in the Tracked Files
Warnings reference
|
sarah11918
left a comment
There was a problem hiding this comment.
Thanks @florian-lefebvre ! I left some questions below to help tease out exactly what's happening.
|
|
||
| The `getActionPath()` utility is used to get the path to an action, taking base and trailing slash settings into account. This is useful when you want to call an action for more advanced usages, such as providing custom headers or not using the fetch API (eg. `navigator.sendBeacon`): | ||
|
|
||
| ```ts |
There was a problem hiding this comment.
Can we have a "this example shows..." and then describe what is happening here?
There was a problem hiding this comment.
Is it worth showing 2 examples? Eg. one using the fetch API and one using the navigator.sendBeacon API
| ```ts | ||
| import { actions, getActionPath } from 'astro:actions' | ||
|
|
||
| const path = getActionPath(actions.like) // '/_actions/like' |
There was a problem hiding this comment.
I'm not sure what the commented part is representing. Is that what is returned by getActionPath(actions.like)?
How is the example different using getActionPath(actions.like) vs just actions.like. You mention that often you can just use the action. What is happening here that we're benefiting from using getActionPath() or that wouldn't otherwise be possible?
There was a problem hiding this comment.
I'm not sure what the commented part is representing. Is that what is returned by getActionPath(actions.like)?
Yes!
How is the example different using getActionPath(actions.like) vs just actions.like. You mention that often you can just use the action. What is happening here that we're benefiting from using getActionPath() or that wouldn't otherwise be possible?
See the keepalive option. I guess it's not clear enough tho, so i'll probably use an example with sendBeacon instead
There was a problem hiding this comment.
See the keepalive option. I guess it's not clear enough tho, so i'll probably use an example with sendBeacon instead
Never answer a question with an example! 😄 I need to know, in your words, what is happening so that I can help make them into docs words! You can never assume that someone will get which part of an example is important, or what exactly you're trying to show. Your example might be fine as an illustration! But it's not docs and not an answer to my specific question "How is this example different using getActionPath than not using it?"
There was a problem hiding this comment.
Ha I see (I think)!
How is this example different using getActionPath than not using it?
Calling an action directly does the following under the hood:
await fetch(someInternalPathLogic, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
})This is internal, which means you can't pass custom headers (eg. you want to protect your actions with a auth header) nor custom options (eg. keepalive which prevents the request from being killed).
Does that help?
There was a problem hiding this comment.
That is very helpful, thank you!
sarah11918
left a comment
There was a problem hiding this comment.
ugh, so sorry I didn't realize these comments were pending!
| ```ts | ||
| import { actions, getActionPath } from 'astro:actions' | ||
|
|
||
| const path = getActionPath(actions.like) // '/_actions/like' |
There was a problem hiding this comment.
See the keepalive option. I guess it's not clear enough tho, so i'll probably use an example with sendBeacon instead
Never answer a question with an example! 😄 I need to know, in your words, what is happening so that I can help make them into docs words! You can never assume that someone will get which part of an example is important, or what exactly you're trying to show. Your example might be fine as an illustration! But it's not docs and not an answer to my specific question "How is this example different using getActionPath than not using it?"
florian-lefebvre
left a comment
There was a problem hiding this comment.
Gotta say I'm struggling to answer correctly your reviews 😄
| ```ts | ||
| import { actions, getActionPath } from 'astro:actions' | ||
|
|
||
| const path = getActionPath(actions.like) // '/_actions/like' |
There was a problem hiding this comment.
Ha I see (I think)!
How is this example different using getActionPath than not using it?
Calling an action directly does the following under the hood:
await fetch(someInternalPathLogic, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
})This is internal, which means you can't pass custom headers (eg. you want to protect your actions with a auth header) nor custom options (eg. keepalive which prevents the request from being killed).
Does that help?
Then they must be the right reviews! 😅 |
|
So last thing worth mentioning is we no longer mention taking base and trailing slash into account, but I'm wondering whether we have to? That could be "true but not particularly helpful to know" in this context? I mean, you get "the URL path you need for your project" and maybe that's enough? Thoughts? |
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
"The URL path you need for your project" sounds more useful to me, people usually expect trailing slash and base to be taken into account |
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
sarah11918
left a comment
There was a problem hiding this comment.
I am really hating GitHub's interface. Here is my stupid review for my stupid pending comment lol. Otherwise, this PR is good to go! 😅
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
Co-authored-by: Sarah Rainsberger <5098874+sarah11918@users.noreply.github.com>
sarah11918
left a comment
There was a problem hiding this comment.
Amazing, good to go @florian-lefebvre ! 🥳
Description (required)
Documents a new
getActionPath()helperRelated issues & labels (optional)