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

Assets cached on production despite having file changes. #2002

Closed
codebykenny opened this issue Jul 22, 2014 · 6 comments
Closed

Assets cached on production despite having file changes. #2002

codebykenny opened this issue Jul 22, 2014 · 6 comments

Comments

@codebykenny
Copy link

Is there a way to make it so browsers force reloading of asset files such as Javascript and CSS files upon there being changes?

Particularly referring to production.css/js

@gavinbunney
Copy link

I've fixed that issue in our app by appending a ?v=<buildNumber> to the assets in sails-linker.js

i.e. edit tasks\config\sails-linker.js, read in some build file (or use a timestamp maybe - less cache friendly), then append it to the fileTmpl as below:

prodJs: {
    options: {
        startTag: '<!--SCRIPTS-->',
        endTag: '<!--SCRIPTS END-->',
        fileTmpl: '<script src="%s?v='+buildDetails.buildNumber+'"></script>',
        appRoot: '.tmp/public'
    },
    files: {
        '.tmp/public/**/*.html': ['.tmp/public/min/production.min.js'],
        'views/**/*.html': ['.tmp/public/min/production.min.js'],
        'views/**/*.ejs': ['.tmp/public/min/production.min.js']
    }
},

@codebykenny
Copy link
Author

Worked like a charm, thank you gavinbunney!

@hasnainvbase
Copy link

Hi ..
can you please help me for clearing the cache problem in my app.
i want to clear cache specially js every first time when page load.

i try this method in my gruntfile.js like code below.
but its not working. its clear cache second time when we refresh page not first time.
can you please help ?

watch: {
api: {

    // API files to watch:
    files: ['api/**/*']
  },
  assets: {
    options: {
        startTag: '<!--SCRIPTS-->',
        endTag: '<!--SCRIPTS END-->',
        fileTmpl: '<script src="%s?v='+buildDetails.buildNumber+'"></script>',
        appRoot: '.tmp/public'
    },
    files: {
        '.tmp/public/**/*.html': ['.tmp/public/js/step1.js'],
        'views/**/*.html': ['.tmp/public/js/step1.js'],
        'views/**/*.ejs': ['.tmp/public/js/step1.js'],
    },
    // Assets to watch:
    //files: ['assets/**/*'],

    // When assets are changed:
    tasks: ['compileAssets', 'linkAssets']
  }
}

@sashasochka
Copy link

How did you actually make use of cache initially? For me nothing is cached in browser, even though server returns 304 code - there still is a round-trip request.

@gavinbunney
Copy link

@sochka a 304 will always make a round-trip request as it is validating that the content hasn't been modified.

In our app instance, we also have middleware that adds cache headers to various requests (depending on what is being requested, cache headers are different), something along the lines of:

var limit = 60 * 60 * 24 * 1000; /* one day */
res.header('Cache-Control', 'public, max-age='+ (limit / 1000));
res.header('Expires', new Date(Date.now() + limit).toUTCString());

@sashasochka
Copy link

@gavinbunney thanks, I've made it work using express.static and maxAge option.

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

No branches or pull requests

5 participants