Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Re-Document Browser Refresh mechanisms #773

Open
tanema opened this issue Sep 1, 2020 · 14 comments
Open

Re-Document Browser Refresh mechanisms #773

tanema opened this issue Sep 1, 2020 · 14 comments

Comments

@tanema
Copy link
Contributor

tanema commented Sep 1, 2020

We have now implemented a notification system that will now notify after the asset has been updated on shopify. It also allows for a webhook notification as well as the file notification. After all of these changes, I think we can document a better browser reload setup.

@felipebrahm provided a good process using webhooks here that we could document in a more formal format. This could either be a tutorial on shopify.dev or just a single page added on the docs site here. #749 (comment)

Process (referenced from above comment)

  • Install a livereload server implementation example (npm install --save-dev tiny-lr)
  • Create a livereload-server.js file:
const tinylr = require('tiny-lr');
const port = 35729; // standard LiveReload port
tinylr().listen(port, () => console.log('Listening on %s', port))
  • Then start the live reload server and theme kit
node livereload-server.js & theme watch --notify="http://localhost:35729/changed"
  • open Chrome and go to my my-website.myshopify.com
  • enable the Live Reload Chrome extension

This would enable live reloading and would even change css files without a reload.

Issues that could be resolved by this documentation

@tarin-swanky
Copy link

Thank you for this fix @tanema, however, I'm a bit confused. The very first time I make a save to a file, browser-sync reloads but it doesn't reload the changes. After the first time however it works. This is my code I currently have in my package.json, I'm using notify

"sync": "concurrently -n \"theme,sync\" -c \"bgBlue.bold,bgGreen.bold\" \"theme watch --allow-live --notify=themeWatchNotify\" \"browser-sync start --proxy 'https://example.myshopify.com/' --files 'themeWatchNotify' --reload-delay 1000 --config bs-config.js\""

I'm wondering why it's not reloading my changes the first time

@tanema
Copy link
Contributor Author

tanema commented Sep 10, 2020

There is no reason why it would work differently on the first request than any other request, though I will take a look around again just in case. Could it be your command that is at fault? Did it work on old versions?

@tarin-swanky
Copy link

I have a feeling it may be the command as when I refresh manually the first time I make changes, the changes that I made come into effect. It just doesn't do it automatically the first time. I am new to this so was hoping someone could give me some pointers on why it may not be working the first time

@tarin-swanky
Copy link

tarin-swanky commented Sep 10, 2020

@tanema I need to add a refresh time of 2000 for it to actually work the first time. Can this be fixed or will there always be a delay time due to Themekit

@tarin-swanky
Copy link

Live reload with Chrome extension can be buggy too. Sometimes it works first time, other times it doesn't. I think it's dependent on whether the website and the chrome extension are all active before making any changes and running the command

@tanema
Copy link
Contributor Author

tanema commented Sep 10, 2020

@tarin-swanky I have left you an urgent comment on one of your repos, you should check your notifications for it.

Which files are you changing? Are they scss or liquid or just plain css/js?

@tarin-swanky
Copy link

@tanema Thank you, I did see. Its only a practice dev site but have put it back to private. I've changed JSON content file to test it, was only changing a couple words here and there to see if it would work. It seems like an issue with Shopify more than anything else. Have tested it many different ways and it always needs a delay, even the live reload server you commented about

@tanema
Copy link
Contributor Author

tanema commented Sep 10, 2020

Okay so if you are changing

  • liquid
  • scss
  • config files (json files in /config)

These trigger a theme rebuild on shopify's server. This may be your delay. Unfortunately there is no way for themekit to know if/when the rebuild would complete. To verify this, try changing a plain js file or plain css file and see if there is a similar delay

@tarin-swanky
Copy link

I thought this may have been the case after testing. Thank you

@felipebrahm
Copy link

felipebrahm commented Sep 11, 2020

@tanema I had been meaning to submit a PR to update the docs or maybe even write a long form blog post explaining all the different ways for developers to improve their Shopify theme local development, but I haven't had the time to do it. I also have some ideas in mind to write a node.js module that could act as a proxy to serve certain files locally (JS, CSS, SCSS) instead of waiting for themekit to upload them. I'll definitely ping you guys if I end up writing that blog post and/or that module I have in mind :)

@felipebrahm
Copy link

@tarin-swanky for me, it works fine with no delay when I'm working on CSS files (I'm doing the SCSS post-processing locally and then just uploading a CSS via themekit). Right now, with the latest themekit beta my live reload script looks like this:

const tinylr = require('tiny-lr');

const LIVE_RELOAD_PORT = 35729;

// Create a live reload server instance
const lrserver = tinylr();

lrserver.listen(LIVE_RELOAD_PORT, (err) => {
  if (err) {
    console.log(`ERROR while trying to listen on port ${LIVE_RELOAD_PORT}`, err);
    return;
  }
  console.log(`Live Reload server started on port ${LIVE_RELOAD_PORT}`);
});

If you need a delay, you could try something like this (this was the script I was using before the latest update for themekit, so I don't really need this anymore):

const chokidar = require('chokidar');
const tinylr = require('tiny-lr');

const LIVE_RELOAD_PORT = 35729;

// Create a live reload server instance
const lrserver = tinylr();

lrserver.listen(LIVE_RELOAD_PORT, (err) => {
  console.log(`Live Reload Server Started on port ${LIVE_RELOAD_PORT}`);
});

// Watch the theme directories (to know which files were changed)
// and the theme.update file (to be notified when themekit has finished uploading changes to Shopify)
const filesToWatch = [
  './assets',
  './config',
  './layout',
  './locales',
  './sections',
  './snippets',
  './templates',
  './tmp/theme.update',
];

const watcher = chokidar.watch(filesToWatch, {
  ignoreInitial: true,
});

let filesChanged = [];

watcher.on('all', (event, path) => {
  if(!/^(add|change)$/.test(event)) {
    // If the event is not "add" or "change" then ignore.
    return;
  }

  // If the file is not theme.update...
  if(!/(^|\/)theme\.update$/.test(path)) {
    console.log(`Change detected: ${path}`);
    // We store which file was changed and we wait to notify the browser once theme.update has changed.
    if(!filesChanged.includes(path)) {
      filesChanged.push(path);
    }
    return;
  }

  console.log(`Notify change detected: ${path}`);

  // If the theme.update file changed, then themekit has finished uploading changes to Shopify, so we tell the browser to reload.
  console.log('Notifying browser to reload', filesChanged);
  notifyBrowser(filesChanged);
  filesChanged = [];
});

const DELAY = 1000;

const notifyBrowser = (files) => {
  setTimeout(() =>
    lrserver.changed({
      body: { files },
    });
  , DELAY);
};

@bigskillet
Copy link

@tarin-swanky, I've noticed this issue too.

I have to manually refresh the first time I make a change, but it works great after that. Adding a reloadDelay seems to solve the first change issue, but running both --notify and reloadDelay kind of defeats the purpose. You have to wait for the notify round trip, then wait for the reload delay, essentially doubling the wait time for each change moving forward. Pretty annoying for sure!

I also noticed that Shopify Slate has the same issue.

@eloyesp
Copy link

eloyesp commented May 12, 2021

Just wanted to share what my current setup. The delay is not fixed, but seems simpler.

@Zhangyinshuai
Copy link

There will still be delays, though, But it solves the live preview problem, Thank you all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants