Skip to content

fix(asset): prevent removal of programmatically created assets#5702

Open
DipakHalkude wants to merge 1 commit into
hexojs:masterfrom
DipakHalkude:fix-asset-generator-removal
Open

fix(asset): prevent removal of programmatically created assets#5702
DipakHalkude wants to merge 1 commit into
hexojs:masterfrom
DipakHalkude:fix-asset-generator-removal

Conversation

@DipakHalkude

@DipakHalkude DipakHalkude commented Oct 8, 2025

Copy link
Copy Markdown

What does it do?

Fixes issue #5680 where programmatically created assets in processors were being removed by the asset generator.

The asset generator was checking for physical file existence and removing all assets without corresponding files, including those created programmatically with modified: true flag.

Solution

Modified the asset generator to skip removal for assets marked with modified: true. This allows plugins and processors to create virtual/assets programmatically that persist in the database.

Changes Made

  1. lib/plugins/generator/asset.ts: Added check for asset.modified flag before removing assets
  2. test/scripts/generators/asset.test.js: Added comprehensive tests

Pull request tasks

  • Add test cases for the changes.
  • Passed the CI test.

Testing

Example Usage

hexo.extend.processor.register("_posts/:slug\.md", function(file) {
  const Asset = this.model('Asset');
  const test = Asset.findById("Test");
  if (!test) {
    return Asset.insert({
      _id: "Test",
      path: "Test",
      modified: true  // This prevents removal
    });
  }
});

@github-actions

github-actions Bot commented Oct 8, 2025

Copy link
Copy Markdown

How to test

git clone -b fix-asset-generator-removal https://github.com/DipakHalkude/hexo.git
cd hexo
npm install
npm test

@DipakHalkude

Copy link
Copy Markdown
Author

@SukkaW @curbengh @D-Sketon Could you please review this fix for #5680? It addresses the issue where programmatic assets weren't persisting in the database.

@stevenjoezhang

Copy link
Copy Markdown
Member

Thanks for working on this.

I don’t think this PR is safe to merge in its current form. The main issue is that modified: true does not mean “programmatically created asset”. In the current Asset schema, modified defaults to true, so many normal file-backed assets also have modified: true.

Because of that, this change breaks the existing cleanup behavior: assets whose source files no longer exist will now be kept instead of removed. The existing test test/scripts/generators/asset.ts already covers this behavior with remove assets which does not exist, and it fails with this PR.

There is also another problem: even when the programmatically inserted asset is preserved, the asset generator still returns a route whose data reads from asset.source using createReadStream(source). For a virtual asset like:

Asset.save({
  _id: 'Test',
  path: 'Test',
  modified: true
});

asset.source points to a physical file path that does not exist, so generation can fail with ENOENT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants