Configuring any CDN to deliver only one file no matter what url has been requested

后端 未结 9 1976
日久生厌
日久生厌 2021-02-01 22:29

I am currently working on a new project where the entire page should be implemented in HTML5/JS working against an API/JSON. Since the entire application should only consist of

9条回答
  •  南旧
    南旧 (楼主)
    2021-02-01 22:41

    I'm in the same boat as you are and it seems that the cdn's are not supporting url rewriting. The following solution does not solve our "problem" exactly but comes very close to saving $ for hosting if you're using a "pull" CDN provider.

    Initial load of the default page (index.html) will provide just a tiny piece of the html, basically the bare-bones html structure, like so:

    
    
    
        something
        
        
    
    
    
    
    
    

    The rest of the code would be loaded via some (async) module loader like require.js -- and all of that code would come from your CDN, including require.js.

    However, even this tiny bit of html in no time will also come from the CDN if you're using pull CDN. The CDN "pull" provider will hit this page whenever it does not find a file for an html5 pushstate url in its cache.

    On your server you have to have some kind of routing to route every request that matches a pattern where a file extension is not provided from the CDN to this one file.

    Yes, the CDN will hit the site every time a new url is encountered (if you're using pull CDN) but after it gets it, it will distribute it to all the users from its cache and will not hit your site for the same url again. Also, the hit on your site from the CDN provider will be insignificant since you're serving a tiny bit of static html. And, if you set your file headers to never expire on this html file (this file should really never change) the file can be kept by the CDN provider for a very long time (depending on the provider), so the hits on your server would pretty much come down to a one time event per a unique url.

提交回复
热议问题