hexo icon indicating copy to clipboard operation
hexo copied to clipboard

`externalLinkFilter` Module-Level Flag Permanently Disables the Feature

Open codeCraft-Ritik opened this issue 2 months ago • 1 comments

Check List

  • [x] I have already read Docs page & Troubleshooting page.
  • [x] I have already searched existing issues and they are not help to me.
  • [x] I examined error or warning messages and it's difficult to solve.
  • [x] I am using the latest version of Hexo. (run hexo version to check)
  • [x] My Node.js is matched the required version.

Expected behavior

The external_link feature should respect the current value of external_link.enable in _config.yml on every page render. If a user enables the feature while hexo server or hexo watch is running, all subsequently rendered pages should immediately have target="_blank" and rel="noopener" added to external links — without requiring a full process restart

Actual behavior

The filter at lib/plugins/filter/after_render/external_link.ts uses a module-level boolean flag to short-circuit execution:

// lib/plugins/filter/after_render/external_link.ts — Lines 4–22
let EXTERNAL_LINK_SITE_ENABLED = true;   // ← Module-level, shared across ALL instances

function externalLinkFilter(this: Hexo, data: string): string {
  if (!EXTERNAL_LINK_SITE_ENABLED) return;   // ← Early exit if ever disabled

  const { external_link, url } = this.config;

  if (!external_link.enable || external_link.field !== 'site') {
    EXTERNAL_LINK_SITE_ENABLED = false;  // ← Permanently flipped! Never resets to true.
    return;
  }
  // ...
}

Once EXTERNAL_LINK_SITE_ENABLED is set to false (the first time the config says the feature is disabled), it can never be reset to true within the same Node.js process. This means:

  • hexo server / hexo watch users: Starting Hexo with external_link.enable: false, then changing to enable: true in _config.yml and saving — the filter remains permanently disabled. External links will never get target="_blank" added until the server is manually restarted.
  • Programmatic users: Running two Hexo instances in the same process (e.g., in CI tests or custom build scripts) — disabling the feature in one instance silently disables it for the other.

How to reproduce?

  1. Start hexo server with this _config.yml:
    external_link:
      enable: false
      field: site
    
  2. Open the browser — confirm external links have no target="_blank" ✅
  3. Edit _config.yml and change to:
    external_link:
      enable: true
      field: site
    
  4. Save the file. Hexo reloads the config.
  5. Open the browser and check — external links still have no target="_blank" ❌
  6. Restart the hexo server process — now external links correctly have target="_blank" ✅

The feature only works after a full restart, not after a live config reload.


Is the problem still there under Safe mode?

Yes. The bug is in the core filter at lib/plugins/filter/after_render/external_link.ts. It is not related to any installed plugin. Safe mode (--safe) does not change this behaviour.

Your Node.js & npm version

node -v && npm -v

> *(Reproducible on any Node.js version)*

Your Hexo and Plugin version

npm ls --depth 0

> Reproducible on Hexo `7.x` and `8.x`.

Your package.json

#### Your `package.json`

{
  "name": "my-blog",
  "version": "1.0.0",
  "dependencies": {
    "hexo": "^8.0.0",
    "hexo-server": "^3.0.0",
    "hexo-renderer-marked": "^6.0.0"
  }
}

Your site's _config.yml (Optional)

url: http://example.com
external_link:
  enable: false   # ← Start with disabled
  field: site
  exclude: ''


---

Others

Root cause (code-level):

Module-level mutable state at lib/plugins/filter/after_render/external_link.ts#L4:

// BUGGY — module-level flag never resets
let EXTERNAL_LINK_SITE_ENABLED = true;

Suggested fix — remove the module-level flag entirely:

// FIXED — always read config at call time; no module-level state
function externalLinkFilter(this: Hexo, data: string): string {
  const { external_link, url } = this.config;
  if (!external_link.enable || external_link.field !== 'site') return data;

  // ... rest of the logic unchanged
}

This is safe: the config object is already cached by Hexo and re-read on every watch-reload cycle. There is no performance benefit to the module-level flag.

codeCraft-Ritik avatar Oct 04 '26 04:10 codeCraft-Ritik

without requiring a full process restart

Does hexo server even support config automatic reload now? Why do you want this to have effect immediately?

SukkaW avatar Oct 06 '26 07:10 SukkaW