Does the app block ID change over time in Shopify?

Does the app block ID change over time in Shopify?

emlez
Shopify Partner
5 0 1

I want to add some custom logic to my store in case a product page has a specific app block.

 

The only way I found to do so, is by getting the `block.id` and compare it as with the ID I see added to the container of said app block, like this:

 

{% liquid
  assign app_block_in_product_page = false
  for block in section.blocks
    if block.type == '@app' and block.id == "06a84c40-89c6-48f1-880a-64ccad40a28b"
      assign app_block_in_product_page = true
      break
    endif
  endfor
%}

 

 

 

And with that variable, conditionally render some elements in my Product pages.

 

But my question is, does this app block ID will get changed randomly in a certain amount of time?

Or was it created when installing the app? So I can rely on it being always the same for my store.

 

Shopify's documentation only states that "The ID is dynamically generated by Shopify.", but would like to know how and if this is subject to change while having the app installed in my store.

Replies 2 (2)

PaulNewton
Shopify Partner
7721 678 1625

It's the same fragile issue as hardcoding generated section ID"s for CSS styles or javascript.

I wouldn't rely on it too many vectors that can break an assumption leading to a false negative: platform updates, cache, app updates, staff editing, app reinstalls, etc.

 

Use alternate templates instead

https://help.shopify.com/en/manual/online-store/themes/os20/theme-structure/templates#create-a-new-t... 

 

If you can't use alt templates , could tighten that loop a bit

{%- liquid
 #block.id should assumed to be fragile.
  assign app_block_in_product_page = false
  assign app_blocks = section.blocks | where: 'type', 'app'
  for block in app_blocks
    if block.id == "06a84c40-89c6-48f1-880a-64ccad40a28b"
      assign app_block_in_product_page = true
      break
    endif
  endfor
-%}

 

And then setup a visual diff text to alert your for a page activating on that logic in case of a change

https://visualping.io/ 

 

If it ever breaks a couple times I'd change the hardcoded ID to a theme setting either global or in the relevant product section settings to not have to muck about in code everytime it breaks.

 

Alternatively test doing it without a loop

 

{%- liquid
 #block.id should assumed to be fragile.
  assign app_block_in_product_page = false
  assign app_block = section.blocks | where: 'type', 'app' | where: 'id', '06a84c40-89c6-48f1-880a-64ccad40a28b'
 if app_block != blank
  assign app_block_in_product_page = true
 endif
-%}

 

test just using 'if app_block ' or against != blank, or != empty , etc

or use map to get all the ids and check with the contains operator.

 

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


emlez
Shopify Partner
5 0 1

Thank you! I'll try to not depend on dynamic values like this one, and your alternative solutions are quite effective!

 

I was also hoping for more clarity on how the "dynamic ID" changes over the app's lifespan, but perhaps only Shopify can provide that information. 🤷🏽