Python flask Static status 304

Asked

Viewed 136 times

1

The flask is returning me the following error:

127.0.0.1 - - [13/Jun/2019 22:55:07] "GET /destino/17 HTTP/1.1" 200 - 
127.0.0.1 - - [13/Jun/2019 22:55:07] "GET /static/css/bootstrap/bootstrap.min.css HTTP/1.1" 304 - 
127.0.0.1 - - [13/Jun/2019 22:55:07] "GET /static/css/bootstrap/dataTables.bootstrap4.min.css HTTP/1.1" 304 - 
127.0.0.1 - - [13/Jun/2019 22:55:07] "GET /static/css/gaia/main.css HTTP/1.1" 304 -

Follow my HTML:

<!DOCTYPE html>
<html lang="pt-br">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <!--<link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap/bootstrap-reboot.min.css') }}">-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bootstrap/bootstrap.min.css') }}">
    <!--<link rel="stylesheet" href="{{ url_for('static', filename='css/bootstrap/bootstrap-grid.min.css') }}">-->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/bootstrap/dataTables.bootstrap4.min.css') }}">
    <!-- Ga.IA -->
    <link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='css/gaia/main.css') }}">
    <title>Ga.IA</title>
</head>
<body>

    <div class="container">
    {% block content %}
    {% endblock %}
    </div>

<footer class="footer">
    <p class="navbar-brand">Versão: {{ sistema['versao'] }} - 2019 (c) - Bruno La Porta</p>
</footer>

    <!-- jQuery primeiro, depois Popper.js, depois Bootstrap JS -->
    <script type="text/plain" src="{{ url_for('static', filename='js/jquery/jquery-3.4.1.min.js') }}"></script>
    <script type="text/plain" src="{{ url_for('static', filename='js/jquery/jquery.mask.min.js') }}"></script>
    <!--<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>-->
    <script type="text/plain" src="{{ url_for('static', filename='js/bootstrap/bootstrap.min.js') }}"></script>
    <!-- DataTable -->
    <script type="text/plain" src="{{ url_for('static', filename='js/jquery/jquery.dataTables.min.js') }}"></script>
    <script type="text/plain" src="{{ url_for('static', filename='js/bootstrap/dataTables.bootstrap4.min.js') }}"></script>
    <!-- Sweet Alert -->
    <script type="text/plain" src="{{ url_for('static', filename='js/sweet_alert/sweetalert.min.js') }}"></script>
    <!-- Ga.IA -->
    <script type="text/plain" src="{{ url_for('static', filename='js/gaia/main.js') }}"></script>
    <script type="text/plain" src="{{ url_for('static', filename='js/gaia/usuario.js') }}"></script>
    <script type="text/plain" src="{{ url_for('static', filename='js/gaia/cliente.js') }}"></script>
</body>
</html>

Note

The scripts javascript should look like type="text/plain", otherwise they would return the same error. I’ve tried using the type in the css and even removing all but the kind of error is the same.

  • Bruno, you tried to clear your browser’s full cache?

  • @THIAGODEBONIS I tried to open in another browser clean, error persisted.

  • Just to ensure try to do a thorough cleaning and after cleaning you press CTRL + R which is the same as F5, then you tell me if you persist.

  • @THIAGODEBONIS, just made, they are returning correctly, however I opened the browser console, it is returning me an error on JQuery and when I try to access a page it accuses error in bootstrap.min.js.

  • Bruno, post this one error in your question, so it becomes easier to help you. So the error current of the pegunta no longer has right?

  • @THIAGODEBONIS, I will ask a further question concerning js and the errors that occur, because it was working, I changed only the type of the links and that’s it, started the problem with the cache and now with the bootstrap and jquery, and these were not solved with this cache cleaning, thank you very much for the tips and the layout, I will open a new question and reference this.

  • I understand, I’ll help you with the next question. Since I was the one who contacted you, I answered your question after this, if you think it right, could you consider the same as the right one and give me a score in it? This is clear if you consider that it is really the right one..

Show 2 more comments

2 answers

4


The code 304 HTTP is not error, otherwise no code in range 3xx will necessarily be error, but all are always referring to redirects.

In case flask already implements E-tag or if-modified system in headers for static files, then in your browser when accessing something static will be saved the values of the mentioned headers, and every time the browser tries to access the same file it sends the "values" back, if the values match the values on the server side, an empty page with code 304 is sent, indicating that the cache copy of the static saved on the user’s computer is the same as the server and therefore the download is no longer necessary.

After that the browser will use the file "cache" as if it had been downloaded.


In a development environment

If you want to avoid caching in development environment you can simply open the console (F12 in Chrome) and select this option:

Disable cache in chrome

If you are using Apache you can put in your . htaccess something like (example only for css and static js):

<filesMatch "\.(?i:css|js)$">
  FileETag None
  <ifModule mod_headers.c>
     Header unset ETag
     Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
     Header set Pragma "no-cache"
     Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
  </ifModule>
</filesMatch>

If it is purely via Flask (command line on the terminal) edit your main flask file similar to this http://flask.pocoo.org/docs/1.0/api/#flask.Flask.get_send_file_max_age of the documentation itself, in your case getting like this for JS and CSS:

class BrunoFlask(flask.Flask):
    def get_send_file_max_age(self, name):
        lname = name.lower()

        if lname.endswith('.js') or lname.endswith('.css'):
            return 0 # tempo de cache dos js e css será de 0 segundos

        return flask.Flask.get_send_file_max_age(self, name)

Note: the use of get_send_file_max_age can serve Apache or Ngnix as well

  • Because then, before it worked without any problem, after I made a change I started to receive this status, I say erro because it just doesn’t work, it’s breaking my application, I even tested in other browsers, but the same result.

  • @bruno101 breaking how? This on apache or straight by flask?

  • @bruno101 see the edition.

  • I’m using directly by flask, but when I play the application for the server, there I use the NGINX, but there was giving no problem, these breaks began to occur in the localhost only, ceio que still fit to ask two questions, because it is returning errors (vi by console) us JS of jquery and bootstrap.

  • @bruno101 breaking how? "Break" can be anything, can’t get it right, what do you mean, the cached CSS doesn’t load? Is that it? Since it is on localhost via terminal uses get_send_file_max_age as I explained in the answer. It should help, but not use in production, ok?

  • I will comment here the error that happens, in one of the pages I have a collapse where I got the example from bootstrap even, it was working until these 3xx returns appeared, now clicking to expand the collapse it shows the following console error (Chrome):

  • bootstrap.min.js:6 Uncaught Typeerror: Cannot read Property 'querySelectorAll' of null at a.t._getParent (bootstrap.min.js:6) at new a (bootstrap.min.js:6) at Htmldivelement.<Anonymous> (bootstrap.min.js:6) at Function.each (jquery-3.4.1.min. js:2) at k.fn.init.each (jquery-3.4.1.min. js:2) at k.fn.init.a._jQueryInterface [as Collapse] (bootstrap.min.js:6) at Htmldivelement.<Anonymous> (bootstrap.min.js:6) at Function.each (jquery-3.4.1.min. js:2) at k.fn.init.each (jquery-3.4.1.min. js:2) at Htmlbuttonelement.<Anonymous> (bootstrap.min.js:6)

  • Caro @bruno101 this there is another error, has nothing to do with cache, simply some error in yours . js: main.js, usuario.js or client.js, we can’t know without which post you tried to do, but let you know, you probably tried to run a script out of $(function () {...}) or without waiting for the page load, so the script tries to access an element that has not yet rendered.

  • I don’t know what one has to do with the other, but they stopped working when I changed the type of css and js, then these began to give this 3xx return and the collapses stopped responding (giving that error), clearing the browser cache css and js back returns 200, but still persists the collapses.

  • I believe it is only right to ask a question concerning js, I will approve the answer and open a new question, thank you very much.

  • Caro @bruno101 does not solve the problem by applying another problem, this is wrong,, by the way the type="text/Plain" is not for scripts, what you have to solve is what I said, start these type="" all of <script>, leave without, and search in your HTML the attribute data-parent= (must be in some "view" your), it must be with a wrong value and so the collapse does not work.

  • Scripts and css links are missing type, as follows now: <script src="{{ url_for('static', filename='js/gaia/main.js') }}"></script>, that is, now this as before these problems occurred, but the remained this residual, could put here in this question too, but I believe that would be very messy.

Show 7 more comments

1

The answer HTTP 304 is for "Redirect to a previously cached result".

That means that the Flask is telling your browser that it already has certain content.

To solve your problem, do the following:

  • Completely clear your browser cache
  • To ensure, after the first step, press the hot key CTRL + R, which will have the same effect as F5.

After these steps your problem will be solved and you will realize that the Flask will return a status 200 at your next request.

Note

I suggest you disable in the development mode of your browser the Cache, so you do not need to remember to clean the same all the time.

  • But you answered everything I had already answered and the problem of the author was not even that. PS: the downvote is not mine, ok? :)

  • @Guilhermenascimento in fact if you check in the comments of the Question, you will notice that I was in contact with the user and everything you said there I had already spoken.

  • Dear Thiago, your comments talk about clearing cache, which incidentally is not the solution and really there doesn’t talk about what is the 304 or anything like that.

  • @Guilhermenascimento actually I already knew the solution, I was only confirming with him, after he had done all the procedures I said solved his problem addressed in the question, the other problem is related to another subject, so much so that he himself affirms and will ask another question for this. But you are with the checara question as the right, do not worry rss.

  • It’s not about score, it’s not about anything like that, it’s about you answering something that’s already been answered, without complementing anything... I don’t fight about score, I’m guiding you, I know a lot of answers, incidentally with technical knowledge, But if someone else has already answered, there’s no point in going there and replying again, unless you complement them. Understands?

  • @I’m not fighting for score either, I’m just trying to explain to you that the problem cited was already in the question, and usually this problem is what we’re talking about, I’m just trying to explain to you that I was already in communication with the user before you responded and was confirming what I had said in the comment to elaborate my response that was already ready. I didn’t see that you had answered as I already had another tab open on the insert answer part. Peace Brow!

  • Dear Thiago I did not say that you are fighting, and neither am I fighting/arguing, fighting for something refers to running after, I do not run after score. Do you understand? I am not arguing with you, I am explaining what may or may not be useful to the community, the rest I explained well in my first and second comments.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.