Skip to content

Allow external <script type=speculationrules> #348

Open

Description

@bramus

I’m building a project that is a bunch of plain HTML files with plain CSS and JS – there is no build step. I’m deploying this project to a server where I cannot control the headers being sent.

Because I can’t control the headers, I needed to add Speculation Rules by adding the following snippet to each and every HTML file:

<script type="speculationrules">
	{
		"prerender": [{
			"source": "document",
			"where": {
				"href_matches": "/*"
			},
			"eagerness": "moderate"
		}]
	}
</script>

This is very repetitive. I kinda assumed I would be able to do the following to keep things DRY, but turns out this is not possible:

<script type="speculationrules" src="file-with-speculation-rules.json"></script>

Hereby requesting to consider this feature request.

Activity

  1. tunetheweb commented on Dec 6, 2024

    @tunetheweb
    Collaborator

    As mentioned on chat, this is possible with plain JS:

    <script src="inject-speculation-rules.js" defer></script>

    And then have inject-speculation-rules.js contain something like this:

    if (HTMLScriptElement.supports &&
        HTMLScriptElement.supports('speculationrules')) {
      const specScript = document.createElement('script');
      specScript.type = 'speculationrules';
      specRules = {
        prerender: [
          {
            urls: ['/next.html'],
          },
        ],
      };
      specScript.textContent = JSON.stringify(specRules);
      console.log('added speculation rules to: next.html');
      document.body.append(specScript);
    }

    But yeah it seems it would be nice to be able to do this directly.

  2. changed the title [-]Allow authors to refer to a standalone JSON file for the speculation rules[/-] [+]Allow external <script type=speculationrules>[/+] on Jan 31, 2025
  3. added
    enhancementNew feature or request
    speculation rulesRelated to speculation rules (syntax, semantics) other than the requested action
    on Jan 31, 2025
  4. tunetheweb commented on Feb 5, 2026

    @tunetheweb
    Collaborator
  5. jspurlin commented on Mar 3, 2026

    @jspurlin

    Microsoft (Office Online) finds this enhancement interesting and thinks it sounds like a great addition to speculation rules!

  6. johannesodland commented on Jun 3, 2026

    @johannesodland

    I came here with the same issue. Working on a site with pure HTML, CSS and JS. No build, no frameworks.

    Would be great to have external speculation rules.

    This goes for all new APIs. It gets very repetitive when configuration has to be repeated on every page.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestspeculation rulesRelated to speculation rules (syntax, semantics) other than the requested action

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions