OK, so I'm starting a new project using Rails 3.1 and I'm new to CoffeeScript.
Anyway, I like the idea of having asset files representing controllers but what if I only want the JS to render when the controller is called?
For example, I have a controller called Game. In my games.js.coffee file, I put some code in there and it's called for every page request. Even pages that have nothing to do with Games.
In Rails 3.0.7, what I would do is put a yield(:js) in the application erb file and then call content_for(:js) in my Games#action view. That way, only the js that was needed for that controller was loaded.
Or, am I going about this the wrong way? Is it better to have ALL js code loaded and cached for every page request to improve performance?
Thanks for any suggestions.
Is it better to have ALL js code loaded and cached for every page request to improve performance?
Basically, the Rails team decided that the answer is usually "yes." Most sites work best with just a single (minified) JS file, which you get by default in Rails 3.1. That way, once the user has accessed a single page of your site, all other pages will load quickly, since all the JS has been cached.
If your site has a huge amount of JS (think Facebook), then I'd suggest architecting your site to load rarely-used JS code asynchronously, using a library like RequireJS. Otherwise, I wouldn't bother loading different code under different controllers; it's a lot of extra work for a marginal benefit.
Take a look at this plugin, I think it solves your problem: https://github.com/snitko/specific_assets
Related
Our clients website load really slow on the first load (the TTFB on the page document can be 10-20s). If I reload the page, the site loads a lot faster.
This may be because of a lot of the files are cached?
Website is here: https://www.mortels.com.au/
This happens for a lot of the pages.
I have tried merging some of the .css files, and will try to attempt the .js files if I cannot find anything else (I never built the original theme, so finding it hard to figure out what is done where and do not have much experience with developing in Shopify.
I also tried adding a lazyloader however it doesn't look like it is working.
Would anyone have any solutions to make the website load quicker? Could it be just the apps we have running on the website causing the initial response to be so slow?
One of the things that can hinder your site's load speed is having too much logic happening through Liquid tags. Shopify has to parse all of the page's Liquid code before it can serve the page, and that has a direct effect on the TTFB
For the files that have unacceptable TTFB ratings, some things you can try to do to help make Shopify's servers serve your content faster include:
Reducing the number of lookups (eg: through all_products[handle] on the page
Avoiding nested for whenever possible
Replacing loops with map whenever you need to make an array of values
Rewriting logic-heavy sections to run in Javascript instead of Liquid (and using the | json filter to drop your liquid variables in a Javascript-friendly version)
Hope this helps!
I'm trying to implement a javascript tracker that i need to inject arbitrarily on all of the views rendered by my application, just like the newrelic client instrumentation works.
My app allows user to edit their liquid html templates, so the idea doing this is to inject the script in a way that the user is uncapable to remove it (auto added)
I look the code in the newrelic gem but is too confusing and i wondering if there is a more simple way to do it.
Thanks in advance!
Well I have a solution for this you could write a middleware where you could just check
if the request is for html page or (css or javascript)
if the request is for the html page
append the javascript to the html page before sending the response for the server
here the catch you need very sure what you are doing this I have ran into this problem
Make sure your middleware placement is correct since the development everything is single thread and wrong placement would result in deadlock error
When HTML page is consider what if the request is an ajax request what then you have to be very specific on that regards
Hope This help
I've noticed that many pages with heavy javascript load certain files at the end so pages load faster, can sprockets help me do this in Rails 3? If not, what methods can I use to help performance when using scripts?
There are a few things to consider here - first, the browser. Most modern browsers can asynchronously load resources, so the old trick of putting javascript at the end of the html isn't as necessary as it once was. Have a read through this article on asynce at css-tricks: http://css-tricks.com/thinking-async/
Next, the point of the asset pipeline is to concatenate, minify and compress all the javascript and css into two files, one 'application.js' and one 'application.css'.
This serves to reduce HTTP requests and means that only the first page load will take
The logic is, that with proper caching, the first load will take longer to get the monster files, but once they've been downloaded, the user has the js and stylesheets cached on their machine.
Have a read through the asset pipeline guide for more information about its use and benefits: http://guides.rubyonrails.org/asset_pipeline.html
When mvc3 was created I placed one of these on my _layout.cshtml page...
#Html.RenderCss()
#Html.RenderScript()
Then in my inheriting razor pages I did this...
#Html.RegisterScript("~/Content/scripts/myscript.js")
Is there now an automatic way to do this with the optimisation dlls that. I dont really want to bundle everything in the folder all the time. Id also like it to minify the file too automatically. Not sure if im missing the point of this bundling feature but i just want to add files dynamically and then have them render out all at the same place on the page...
Thanks...
My understanding of the bundling feature, which is still pre-release and somewhat buggy is that it is designed to combine and then minify all of your CSS or JS files you might need in your project. It then uses a versioning mechanism so that clients get the full browser caching experience.
For example if I have 3 CSS files (Reset.css, Global.css, and Site.css) these all get downloaded on the first visit to my site, they are combined and minified so the file size is quite small. The url contains a special hash number/querystring. As long as I don't change the content the cached copy stays on the client until it expires or they clear their temp files. When I make a change the old version is discarded and a new hash is created. When the client visits again their file is now invalid and it redownloads the new file.
I personally have not tried to create specific bundles but with a search I found someone who has created a custom bundle, I think this may be along the lines of what you are trying to accomplish: http://www.codeproject.com/Tips/389545/ASP-NET-MVC4-Bundling-and-Minification
I'd like to create a userguide for the application I'm building using the Kohana framework, and I'm wondering if there's a way I can use the Kohana userguide module for this purpose.
I understand how to add userguide info for new modules that I create, and how to include my classes in the API, but I want to build a second, separate userguide for the actual application user, as opposed to the app developers.
At first, I thought I'd just try adding app help pages to the main userguide at APPPATH/guide. I tried adding a "application/guide" directory, and put a file in there called menu.md, but that just ended up replacing the Kohana menu in the userguide. After renaming the file to menu.myapp.md, it doesn't show up at all.
So then it occurs to me that I could simple edit modules/userguide/guide/menu.md to add sections for my app, and likewise add markdown files for each app component. But really it would be much better to have a completely separate userguide for app users since the Kohana documentation isn't relevant for them.
What's the best way to go about this? Should I create a duplicate of the entire userguide module and modify the routing, &c.? Or is there some way to set up both userguides using the one version of the module? Or am I barking up the wrong tree altogether? Is there some other module/approach that would be better for building "Help" pages for the app?
Thanks in advance for your help!
Yes, you can make docs for your application with the userguide. If you want examples, check out these links:
https://github.com/zombor/Auto-Modeler/blob/master/config/userguide.php
https://github.com/zombor/Auto-Modeler/tree/master/guide/auto-modeler
Note that you'll still get "api docs" and everything else, unless you change the config to hide them.