How to Implement HSTS into your Hydrogen Project

I am a developer from North Carolina. I have always been fascinated by the internet, and scince high school I have been trying to understand the magic behind it all. ECU Computer Science Graduate
Search for a command to run...

I am a developer from North Carolina. I have always been fascinated by the internet, and scince high school I have been trying to understand the magic behind it all. ECU Computer Science Graduate
No comments yet. Be the first to comment.
As a technical person, it’s always been easy for me to focus on hard skills, numbers, and statistics. This worked well through school, personal projects and smaller teams; environments where I could accomplish a ton on my own. But now I’m working in ...

I recently wrote an article detailing how to embed Yotpo’s loyalty widgets on a headless site. As I was writing it, I thought “there is no way that the reviews widget will be this troublesome”. I was wrong, it was just as troublesome. For 2 primary r...

Currently, I am working on turning a Liquid-based Shopify store into a headless Hydrogen-based store. Part of this was transferring Yotpo embedded forms to the new headless site. As for the loyalty and referrals portion of Yotpo, you can embed these ...

Within Shopify, store owners are restricted from having full control of their store’s checkout page. This is a safety feature on Shopify’s end, protectecting store owners and customers from accidentally (or purposely) exposing private information. Th...

HSTS is HTTPS Strict-Transport-Security. It enforces that only HTTPS is used across your web application, preventing unencrypted traffic being monitored and collected by unwanted entities.
It can be enabled in your Hydrogen app via response headers. These response headers are found in your entry.server.js file. At the bottom of the handleRequest function in the entry.server.js file, you will see lines like so:
responseHeaders.set('Content-Type', 'text/html');
responseHeaders.set('Content-Security-Policy', header);
These lines of code control the headers that are returned with the page content to the users. We can add some headers to ensure HSTS, making sure the application delivered to the client’s machine ONLY sends encrypted traffic through HTTPS
// HSTS header
responseHeaders.set(
'Strict-Transport-Security',
'max-age=31536000; includeSubDomains; preload',
);
// BONUS: Prevent content sniffing
responseHeaders.set('X-Content-Type-Options', 'nosniff');
The max-age means the amount of time (in seconds) that the browser should enforce the HTTPS-only policy for the domain, the includeSubDomains tells us that the policy applies to all subdomains of the specified domain, and preload denotes that the domain can be included in browsers' HSTS preload lists to enforce HTTPS by default without the user visiting the site first.
And at the bottom, I also added a simple extra header to prevent content sniffing!