Skip to content

Rework the mechanism of copying server asset as client asset copy #1109

@hi-ogawa

Description

@hi-ogawa

Related plugins

Description

Currently rsc plugin copies all assets from rsc environment to client environment by default.

for (const asset of Object.values(manager.bundles['rsc']!)) {
if (asset.fileName === rscViteManifest) continue
if (asset.type === 'asset' && filterAssets(asset.fileName)) {
this.emitFile({
type: 'asset',
fileName: asset.fileName,
source: asset.source,
})
}
}

This is mainly for copying css assets on server that are handled internally by rsc plugin but also end-user's ?url import inside rsc environment.

However this is not a safe heuristics since rsc environment is the server logic which should get exposed as public assets. As one such example, rscViteManifest is avoided to get copied above.

For this concern, rsc plugin provides copyServerAssetsToClient option to control which gets copied, but this option isn't composable and more like targetted as end-user's tweak to filter out something.

/**
* This option allows customizing how client build copies assets from server build.
* By default, all assets are copied, but frameworks can establish server asset convention
* to tighten security using this option.
*/
copyServerAssetsToClient?: (fileName: string) => boolean

Suggested solution

Explore whether we can improve default heuristics to only detect assets which are clearly intended as public assets i.e. side effect css import and ?url import in rsc environment.

If user plugins employs emitFile({ type: "asset" }) in rsc environment, they shouldn't get copied to public assets in client environment by default, but that should be opt-in behavior. For example, Astro seems to have introduced emitClientAsset withastro/astro#15345 for such behavior possible.

Alternative

No response

Additional context

No response

Validations

Metadata

Metadata

Assignees

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions