0

Is there any way to get a 304 Not Modified (as opposed to a 200 and the file's contents) response from the file system if, for instance, I access file:///img.png and the browser already has this file cached?

I mean this as in, can I do this without setting up a file server that communicates over http? Or, is this just a limitation of the file protocol.

eatonphil
  • 13,115
  • 27
  • 76
  • 133

1 Answers1

1

You cannot. It is a limitation of the file: protocol. In fact, that protocol does not define 200 or 304 codes. Those codes are specific to the http protocol.

As to whether the browser may use a cached copy of the file, the client is responsible for loading the file resource itself. There is no server component to know whether or not the file was modified. The only component in the interaction that could know is the browser, and the only way for it to know for sure is to load the file.

It could check the timestamp in theory, and perhaps some browsers do, but having the same timestamp as a previous load does not guarantee the file is unmodified. At least on some systems, the last modified time can be changed via a system call.

Eric J.
  • 147,927
  • 63
  • 340
  • 553
  • Is it unrealistic for the browser to use the file system to check the last-modified data? I know I can get that data on a Unix platform. – eatonphil Jun 08 '15 at 18:16
  • A browser could, but the last modified date can often be set by a system call. It's not completely reliable on all systems for that reason. Note that codes 200 and 304 are HTTP codes, not applicable to the file: protocol. I updated my answer to make that clearer. – Eric J. Jun 08 '15 at 18:31
  • Maybe so, but the browser still tells me I'm getting a "200" response (in the network tab) when I request a file using the file protocol. – eatonphil Jun 08 '15 at 18:33
  • IE on Windows doesn't do that. The 200 code is not an "official" code for the file protocol, though a browser may elect to indicate that code. "I loaded the file" is functionally about the same as an HTTP 200. You can test if your browser on your OS is caching by loading a rather large file and using a tool like iostat to see if the file is actually being read on a second access. There's nothing cross-platform, cross-browser though to get at what you want in a manner guaranteed by a spec. – Eric J. Jun 08 '15 at 18:37