Skip to content

Instantly share code, notes, and snippets.

@rahsheen
Created November 10, 2020 02:39
Show Gist options
  • Save rahsheen/80e0c4e65f69c019ea5278b0af743f9f to your computer and use it in GitHub Desktop.
Save rahsheen/80e0c4e65f69c019ea5278b0af743f9f to your computer and use it in GitHub Desktop.
BrowserSync Config for Shopify (Slate) Which Can Be Modified and Used with Themekit
const browserSync = require('browser-sync');
const {getStoreValue, getThemeIdValue} = require('@shopify/slate-env');
const {getSSLKeyPath, getSSLCertPath} = require('../utilities');
class DevServer {
constructor(options) {
this.bs = browserSync.create();
this.target = `https://${getStoreValue()}`;
this.themeId = getThemeIdValue();
this.port = options.port;
this.address = options.address;
this.uiPort = options.uiPort;
this.proxyTarget =
this.target +
(this.themeId === 'live' ? '' : `?preview_theme_id=${this.themeId}`);
}
start() {
const bsConfig = {
port: this.port,
proxy: {
target: this.proxyTarget,
middleware: (req, res, next) => {
// Shopify sites with redirection enabled for custom domains force redirection
// to that domain. `?_fd=0` prevents that forwarding.
// ?pb=0 hides the Shopify preview bar
const prefix = req.url.indexOf('?') > -1 ? '&' : '?';
const queryStringComponents = ['_fd=0&pb=0'];
req.url += prefix + queryStringComponents.join('&');
next();
},
},
// Fix analytics injection bug
snippetOptions: {
rule: {
match: /<\/body>/i,
fn: function (snippet, match) {
return snippet + match;
}
}
},
https: {key: getSSLKeyPath(), cert: getSSLCertPath()},
logLevel: 'silent',
socket: {
domain: `https://${this.address}:${this.port}`,
},
ui: {
port: this.uiPort,
},
};
return new Promise((resolve) => {
this.server = this.bs.init(bsConfig, resolve);
});
}
}
module.exports = DevServer;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment