Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow external <script type=speculationrules> #348

Open
bramus opened this issue Dec 6, 2024 · 1 comment
Open

Allow external <script type=speculationrules> #348

bramus opened this issue Dec 6, 2024 · 1 comment
Labels
enhancement New feature or request speculation rules Related to speculation rules (syntax, semantics) other than the requested action

Comments

@bramus
Copy link

bramus commented Dec 6, 2024

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.

@tunetheweb
Copy link

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.

@domenic domenic changed the title Allow authors to refer to a standalone JSON file for the speculation rules Allow external <script type=speculationrules> Jan 31, 2025
@domenic domenic added enhancement New feature or request speculation rules Related to speculation rules (syntax, semantics) other than the requested action labels Jan 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request speculation rules Related to speculation rules (syntax, semantics) other than the requested action
Projects
None yet
Development

No branches or pull requests

3 participants