0

I trying to build a website that serves mostly static content, aside from raw data.
Which means that instead of having the web server create the raw HTML code from data from the database, I want the server to send an initial static frame website that contains js code which then loads the data and posts and such from a REST API on the server and builds the posts on the website from that.
The ultimate goal of this approach is that the browser ultimately caches the site after the first download and from then on only has to load raw data from the server, thus easing the strain on the server.

The problem with this approach is theres no elegant way to serve the basic framework elements of the page (header, body, footer, etc ) piecemeal without using some form of autoconcatenation, like djangos templates for example, which I am purposefully trying to avoid for the above reasons.
I know a while ago chrome introduced

<link href="extern.html" rel="import" />

which would be absolutely perfect, but unfortunately I doesn't work on most browsers (even chrome without config changes).

So basicly the question is, how do elegantly serve exclusively static web content and have the website build itself from the initial frame document, without using templates and such? which would be absolutely perfect for this,

user2741831
  • 2,120
  • 2
  • 22
  • 43
  • Why can't you use a django template to serve the first page? Note that there are various caching mechanisms, including using a CDN to cache the first page so server doesn't get hit after it has served the page once. On an easier level, you can add caching headers to the request for the home page telling the browser to cache the page indefinitely. – dirkgroten Sep 04 '19 at 10:11
  • TL;DR you're asking the wrong question. – dirkgroten Sep 04 '19 at 10:12
  • I don't get what you mean. If the page is dynamically generated and not static content like a css file, how can django know beforehand whether or not to cache it? Also creating a CDN network seams like total overkill for my project. – user2741831 Sep 04 '19 at 10:21
  • also this seams to solve my problem pretty elegantly without having to change much in the backend, so I might close the thread: https://stackoverflow.com/questions/9963799/ajax-jquery-load-webpage-content-into-a-div-on-page-load – user2741831 Sep 04 '19 at 10:23
  • Well, you have a view that returns that page, so in the code for that specific view you can [tell Django with `@cache_control`](https://docs.djangoproject.com/en/stable/topics/cache/#controlling-cache-using-other-headers) to add the headers to the response so that the browser caches the response. – dirkgroten Sep 04 '19 at 10:26
  • And if CDN is overkill, then "strain on your server" is probably not relevant, since CDN is the first thing you do when there is strain on your server. – dirkgroten Sep 04 '19 at 10:27
  • I just didn't know it was possible to cache nonstatic content, so ill look into that, thanks. – user2741831 Sep 04 '19 at 10:52
  • What do you mean by "non static"? if you view needs to fetch dynamic data from the database, then of course you shouldn't cache it. But if your view is just rendering a template without adding any dynamic data, then it's actually returning something that the browser can cache and so you can instruct it to do so. – dirkgroten Sep 04 '19 at 11:02
  • The only thing you have to be really careful of, is if you want to make changes to the HTML of the page, and the browser is caching it indefinitely, then your users won't see the changes. This means your url should change, or just cache it for one hour (3600s), or the average time a user is using your page in one session. This is by the way the same as for the static files included in your HTML (the js, css and images). – dirkgroten Sep 04 '19 at 11:04
  • By static I simply mean files in the /static/ folder. So basically I just have to make sure that they are "static", in that they are independent of the database and that I have to include some sort of version number in the header, incase I make changes? Also I can't cache the inital html page, since that one should always relay the newest content? Also If I limit it to an hour, the server would still have to resend the site almost every session, which I guess is okay. But thanks, if it works the way I think it does this could work out, thanks – user2741831 Sep 04 '19 at 11:10
  • But your django server won't serve the static files in production anyway. That's something your nginx or apache server does (and you tell nginx to add the appropriate cache headers). And yes, if you change a js file, you need to change its name. Django has a [`ManifestStaticFilesStorage`](https://docs.djangoproject.com/en/2.2/ref/contrib/staticfiles/#manifeststaticfilesstorage) that takes care of that when you collect static in the location where you'll serve your static files. – dirkgroten Sep 04 '19 at 11:13
  • That usefull, thanks. – user2741831 Sep 04 '19 at 11:24

0 Answers0