htmx icon indicating copy to clipboard operation
htmx copied to clipboard

Bug with multiple pages + history back + <body> tag

Open josephernest opened this issue 3 years ago • 0 comments
trafficstars

I found a strange problem, I spent some time isolating it, here is a minimal example (I've cut the most I could).

How to reproduce:

  • Launch the server.py (requirement pip install bottle)
  • Go with Chrome to http://127.0.0.1:8080/pages/index
  • Click on "Go to page1"
  • Click "Browser back" button
  • Click on "Go to page1" again
  • Click "Browser back" button: bug: you are staying on page1 instead of going back in history.

Ready to test ZIP file: https://gget.it/n4qu/htmx_multipages_bottle2.zip

All the files are here:

server.py:

import bottle
app = bottle.Bottle("")
@app.route('/pages/<path>')
def main(path):
    fullpage = bottle.request.headers.get('HX-Request') is None
    return bottle.template(path + '.html', fullpage=fullpage)
@app.route('/static/<path>')
def static(path):
    return bottle.static_file(path, root='.')
app.run()

header.html

<body>
<script src="/static/htmx.min.js"></script>
<div class="main">
    <div class="header">
        HEADER
    </div>

footer.html:

    <div id="footer">
        FOOTER
    </div>
</div>
</body>

index.html:

% if fullpage: 
%     include('header.html')
% end
<div id="container">
Hello world!
<a hx-get="/pages/page1" hx-swap="outerHTML" hx-target="#container" hx-push-url="true">Go to page1</a>
</div>
% if fullpage:
%     include('footer.html')
% end

page1.html:

% if fullpage: 
%     include('header.html')
% end
<div id="container">
This is page1
</div>
% if fullpage:
%     include('footer.html')
% end
  • Strange fix 1: remove <body> and </body> in the header and footer, and then it works (!)
  • Fix 2 : replace HTMX 1.7.0 by HTMX 1.8.0 and then it works. Has something been modified between these 2 versions?

Any idea?

josephernest avatar Jul 19 '22 13:07 josephernest