Skip to content

fix(actions): support trailing slash#12657

Merged
ematipico merged 2 commits into
withastro:mainfrom
darkmaga:actions-trailing-slash
Dec 11, 2024
Merged

fix(actions): support trailing slash#12657
ematipico merged 2 commits into
withastro:mainfrom
darkmaga:actions-trailing-slash

Conversation

@darkmaga

@darkmaga darkmaga commented Dec 5, 2024

Copy link
Copy Markdown
Contributor

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

@changeset-bot

changeset-bot Bot commented Dec 5, 2024

Copy link
Copy Markdown

🦋 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
Comment thread packages/astro/src/actions/plugins.ts Outdated
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
Copy Markdown
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
Copy Markdown
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

@codspeed-hq

codspeed-hq Bot commented Dec 6, 2024

Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

To remove?

Comment thread packages/astro/src/actions/plugins.ts Outdated
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
Copy Markdown
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

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