Template values encoding URLs in Sanity starter templates

16 replies
Last updated: Apr 15, 2021
I am currently creating a new starter template and want to use template-values for presetting some stuff.There are also urls in my template values. Unfortunately after replacing the values these are shown as encoded string. Is there any trick to solve this?
e.g.

<https://sanity>...
will become:

https:&#x2F;&#x2F;sanity-...
This happens when running:

sanity-template build --template-values template-values-development.json
AI Update

I understand the issue you're running into! When using sanity-template build --template-values, URLs in your template are being HTML-encoded, turning slashes (/) into &#x2F; entities. This is a known behavior with how the template replacement works.

The Issue

The sanity-template tool uses a templating engine that HTML-encodes values by default for safety. When you have URLs like https://sanity-... in your template values JSON file, the forward slashes get converted to &#x2F; during the build process.

The Solution

Unfortunately, the sanity-template repository has been archived since March 6, 2023, meaning it's no longer actively maintained. However, here are a few workarounds:

  1. Use triple braces (if supported): Some templating engines use triple braces {{{variable}}} instead of double braces to avoid HTML encoding. Check if your template syntax supports this.

  2. Post-process the build output: After running the build command, you could run a script to decode the HTML entities:

    # After building
    find ./build -type f -exec sed -i 's/&#x2F;/\//g' {} +
  3. Manual replacement in template files: Instead of putting full URLs in your template-values JSON, consider:

    • Breaking URLs into parts (protocol, domain, path separately)
    • Using a placeholder that doesn't require encoding
    • Handling URL construction in your application code after template generation
  4. Fork and modify: Since the tool is archived, you could fork the repository and modify the template replacement logic to handle URLs without encoding them.

Important Note: Given that this tool is archived and no longer maintained by Sanity, you might want to consider alternative approaches for creating starter templates, such as using a more modern templating solution or build tool that gives you better control over encoding behavior.

If you share more details about your specific use case, I can suggest more targeted solutions!

Show original thread
16 replies
Hi
user A
Thanks for your reply. I just updated the version of sanity-template. Unfortunately with the same result:

https:&#x2F;&#x2F;sanity...
Hi
user A
I just updated the verison of sanity-template. Unfortunately with the same result:

https:&#x2F;&#x2F;sanity...
Hm, what template field is this and what type of file is it? (json or js/ts)
It's the scully.web.config.ts file
setPluginConfig(SitemapPlugin, {
    urlPrefix: 'https:&#x2F;&#x2F;sanity-....,
    ...
})

Just to double check, if you run
sanity-template --version
and
npx sanity-template --version
you get
2.4.2
from both?
npx sanity-template --version shows 2.4.2sanity-template --version shows 2.4.1

how do I need to update it to have a 2.4.2 for both?
npx sanity-template --version shows 2.4.2sanity-template --version shows 2.4.1

how do I need to update it to have a 2.4.2 for both?
you don't need the global one if you always run
npx sanity-template ...
which is a good habit (in my opinion, at least 🙂)
so you can either remove the global (
npm rm -g sanity-template
) or upgrade it (
npm install -g sanity-template
)
True! This works. Thanks a lot!By the way I get an error in

node_modules\sanity-template\dist\commands\utils\fs.js:67:39
I solved this temporary by just returning an empty array in the resolveIgnores function.
I am running this on a win10 64bit machine inside an nx workspace.
Maybe you can note this as an issue at sanity.
Nevertheless now I get the expected result from template builder.
Thanks a lot!
Glad to hear it worked!
Glad to hear it worked!
If you got time we'd be very thankful if you could report the fs issue (preferably with a full stack trace) on https://github.com/sanity-io/sanity-template/issues
alright, I created an issue. Hopefully there's enough information in it.Again thanks a lot for your support
Thanks a lot to you too!

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?