I have a hubDB database to build dynamic pages with multiple resources (analyst reports, whitepapers etc. I have a single select column "resource_type.
I've noticed that if I take the end slug from the resource and put it on the end of the example.com/resource/whitepaper/my-slug or /resource/analyst-report/my-slug, it will render regardless because they are in the same database.
So I have added code to ID what resource it is, but now I end up with a blank page when it isn't the correct resource. Nav and footer, but no body. I've tried to do an else and force a 404, but have had no luck.
Can someone give me some help on what the best practice would be in this situation? The code (screenshot) gives me a blank page. If I add ?hsCacheBuster=1234, it shows the 404, but I think that is because it is refreshing the page. It has been 24 hours, so I don't think it is a caching issue.
The main problem lies in the logic where it is finding an exact page match for the slug. i.e. dynamic_page_hubdb_row exists, but the problem is that it doesn't match that template that it is built for, like here, it is for "whitepaper", and the slug is for "analyst-reports". This is why the condition fails and skips everything except nav and footer. If the dynamic_page_hubdb_row exists but is not the same type, it also won't give a 404. What you have to do is explicitly return a 404 page when resource_type.name is not matched.
{% if dynamic_page_hubdb_row %}
{% if dynamic_page_hubdb_row.resource_type.name == 'whitepapers' %}
<!-- Render the whitepapers content -->
<div class="whitepapers-main-cont container">
<!-- existing hero, etc. -->
</div>
{% else %}
{{ set_response_code(404) }}
<div class="error-container">
<h1>404 - Not Found</h1>
<p>The resource you are looking for does not exist or is of the wrong type.</p>
</div>
{% endif %}
{% elif dynamic_page_hubdb_table_id %}
<!-- Index listing page -->
<div class="resources-hero resource-hero-bg">...</div>
<div class="resource-main-container container">
<!-- listing logic -->
</div>
{% else %}
{{ set_response_code(404) }}
<div class="error-container">
<h1>404 - Not Found</h1>
</div>
{% endif %}
I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.
The main problem lies in the logic where it is finding an exact page match for the slug. i.e. dynamic_page_hubdb_row exists, but the problem is that it doesn't match that template that it is built for, like here, it is for "whitepaper", and the slug is for "analyst-reports". This is why the condition fails and skips everything except nav and footer. If the dynamic_page_hubdb_row exists but is not the same type, it also won't give a 404. What you have to do is explicitly return a 404 page when resource_type.name is not matched.
{% if dynamic_page_hubdb_row %}
{% if dynamic_page_hubdb_row.resource_type.name == 'whitepapers' %}
<!-- Render the whitepapers content -->
<div class="whitepapers-main-cont container">
<!-- existing hero, etc. -->
</div>
{% else %}
{{ set_response_code(404) }}
<div class="error-container">
<h1>404 - Not Found</h1>
<p>The resource you are looking for does not exist or is of the wrong type.</p>
</div>
{% endif %}
{% elif dynamic_page_hubdb_table_id %}
<!-- Index listing page -->
<div class="resources-hero resource-hero-bg">...</div>
<div class="resource-main-container container">
<!-- listing logic -->
</div>
{% else %}
{{ set_response_code(404) }}
<div class="error-container">
<h1>404 - Not Found</h1>
</div>
{% endif %}
I hope this will help you out. Please mark it as Solution Accepted and upvote to help another Community member.
Thanks for reaching out with this! This is a common challenge when setting up dynamic pages with HubDB, especially when dealing with different resource types from a single table. Hitting that blank page is definitely frustrating.
I'm tagging a few community members who can help with this: @kosalaindrasiri@SteveHTM@GiantFocal Hopefully, they can provide some insights to help you sort this out!