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

fix(actions): support trailing slash #12657

Merged
merged 2 commits into from
Dec 11, 2024

Conversation

darkmaga
Copy link
Contributor

@darkmaga darkmaga commented Dec 5, 2024

Changes

Closes withastro/adapters#464

Problem: node adapter doesnt pass astro actions request and always redirect this to path with the / in the end

image
image
image

Solution: add trailing slash to actions url in case trailingSlash: "always" to pass node adapter static handler

Testing

Tested with node adapter and add new test for server actions handler

Docs

Probably no need to update docs

Copy link

changeset-bot bot commented Dec 5, 2024

🦋 Changeset detected

Latest commit: 35ba7e5

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added the pkg: astro Related to the core `astro` package (scope) label Dec 5, 2024
@@ -84,6 +84,7 @@ export function vitePluginActions({
code += `\nexport * from 'astro/actions/runtime/virtual/server.js';`;
} else {
code += `\nexport * from 'astro/actions/runtime/virtual/client.js';`;
code = code.replace('__TRAILING_SLASH__', JSON.stringify(settings.config.trailingSlash === 'always'));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this is a correct way to pass config settings

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually, inside the templates, we have comments like this, which will be then used as placeholders to replace with concrete values by the vite plugins:

// @@ON_SET_GET_ENV@@

/* @@LOOKUP_MAP_ASSIGNMENT@@ */

Do you think we might be able to use the same convention?

Maybe we can refactor the template code in actions.mjs like this:

const actionComponentPath =  /** @TRAILING_SLASH@ **/ ;
const actionPath = import.meta.env.BASE_URL.replace(/\/$/, '')} + '/_actions/'  + path + actionComponentPath

Copy link

codspeed-hq bot commented Dec 6, 2024

CodSpeed Performance Report

Merging #12657 will not alter performance

Comparing darkmaga:actions-trailing-slash (35ba7e5) with main (e21c7e6)

Summary

✅ 4 untouched benchmarks

Comment on lines 284 to 288
const pipeline: Pipeline = Reflect.get(context, apiContextRoutesSymbol);

const baseAction = await getAction(
pipeline.manifest.trailingSlash === 'always' ? callerInfo.name.replace(/\/$/, '') : callerInfo.name,
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have three utilities for manage the trailing slash, you should use them. Here's an example from the source code:

pathname = shouldAppendForwardSlash(trailingSlash, buildFormat)
? appendForwardSlash(newUrl.pathname)
: removeTrailingForwardSlash(newUrl.pathname);

const baseAction = await getAction(
pipeline.manifest.trailingSlash === 'always' ? callerInfo.name.replace(/\/$/, '') : callerInfo.name,
);
// const baseAction = await getAction(callerInfo.name);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To remove?

@@ -84,6 +84,7 @@ export function vitePluginActions({
code += `\nexport * from 'astro/actions/runtime/virtual/server.js';`;
} else {
code += `\nexport * from 'astro/actions/runtime/virtual/client.js';`;
code = code.replace('__TRAILING_SLASH__', JSON.stringify(settings.config.trailingSlash === 'always'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually, inside the templates, we have comments like this, which will be then used as placeholders to replace with concrete values by the vite plugins:

// @@ON_SET_GET_ENV@@

/* @@LOOKUP_MAP_ASSIGNMENT@@ */

Do you think we might be able to use the same convention?

Maybe we can refactor the template code in actions.mjs like this:

const actionComponentPath =  /** @TRAILING_SLASH@ **/ ;
const actionPath = import.meta.env.BASE_URL.replace(/\/$/, '')} + '/_actions/'  + path + actionComponentPath

@ematipico ematipico merged commit 14dffcc into withastro:main Dec 11, 2024
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: astro Related to the core `astro` package (scope)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Server returns 301 for server actions with config trailingComa: "always" in preview mode
2 participants