Can JSDOM work with Liquid for inserting code into a Shopify theme using Asset API?

d_tehrani
Shopify Partner
56 1 13

Hi.
I'm working on an app and been trying to insert a code into a theme file from my app.

 

I'm attempting to do something like this, but it doesn't work since JSDOM does not work properly with Liquid. 

 

 

const productTemplateFile
  = `
  <div class="product-template__container page-width" id="ProductSection-{{ section.id }}" data-section-id="{{ section.id }}" data-section-type="product" data-enable-history-state="true" data-ajax-enabled="{{ settings.enable_ajax }}">
    product page template file
  </div>
  `
  
const dom = new JSDOM(productTemplateFile);
const parent 
  = dom.window.document.querySelector(".product-template__container" );

const childElement = dom.window.document.createElement("div");
childElement.textContent = child
parent.appendChild(childElement)

const uri = `https://${shopOrigin}/admin/api/${API_VERSION}/themes/${themeId}/assets.json`;

const body = JSON.stringify({
  asset: {
    key: "sections/product-template.liquid",
    value: dom.window.document.body.innerHTML
  }
)

await fetch(uri, {
  method: "PUT",
  headers: { "X-Shopify-Access-Token": accessToken },
  body
  }
});

 

 

 

Is there a way to make JSDOM work with Liquid, or are there other ways to insert code into a theme using the Asset API?

Thanks.

Replies 0 (0)