Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] HTMX getting incorrect base url on page load #2106

Open
Cethannel opened this issue Dec 19, 2023 · 0 comments
Open

[BUG] HTMX getting incorrect base url on page load #2106

Cethannel opened this issue Dec 19, 2023 · 0 comments

Comments

@Cethannel
Copy link

I site at https://myurl.com/select_block_id which gets redirected to by the nginx rule:

        location /select_block_id/ {
                rewrite ^/select_block_id/(.*)$ /$1 break;
                proxy_pass http://localhost:42059;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header X-Forwarded-Proto $scheme;
        }

This directs to the page:

<!doctype html>
<html>

<head>
  <script src="https://unpkg.com/htmx.org@1.9.9"></script>
</head>

<body>
  <div hx-get="/form" hx-trigger="load">Click Me</div>

  <script>
    console.log('Root: ', window.location.href);
    async function thing() {
      const response = await fetch('/select_block_id');
      const text = await response.text();
      console.log('Response: ', text);
    }
    thing();
  </script>

</body>

</html>

However on load I get a CORS error because my nginx redirects to an external url for root url (https://myurl.com/).

The Root console log returns https://myurl.com/select_block_id/ and the fetch request returns the correct html for the page.

I believe that the issue is caused by htmx not correctly detecting the base url.

I can resolve this by this to the script tag:

    document.body.addEventListener('htmx:configRequest', (event) => {
      event.detail.path = `/select_block_id${event.detail.path}`
    })
@Cethannel Cethannel changed the title HTMX getting incorrect base url on page load [BUG] HTMX getting incorrect base url on page load Dec 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant