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

Script with innerHTML not working on Safari #4859

Closed
1 task done
aleksa-codes opened this issue Sep 24, 2022 · 12 comments · Fixed by #4861
Closed
1 task done

Script with innerHTML not working on Safari #4859

aleksa-codes opened this issue Sep 24, 2022 · 12 comments · Fixed by #4861

Comments

@aleksa-codes
Copy link
Contributor

aleksa-codes commented Sep 24, 2022

What version of astro are you using?

latest

Are you using an SSR adapter? If so, which one?

None

What package manager are you using?

yarn

What operating system are you using?

Windows, but the issue is on iOS mobile

Describe the Bug

Trying to get a simple message displayed in the footer saying "Have a great {currentDay}" in my Footer astro component.

In doing so, I first tried writing the function for getting the current day in the Component Script part (---) and that didn't work, it would only get the day when the site was built and deployed, which is as I understand the expected outcome.

Then I realized I should probably run that script on the client side because I need the current day to be updated daily, so I used the <script> tag with the same function and injected the text with .innerHtml . That got the desired outcome and the days were updating as expected, everything was working on Desktop browsers. But when I opened the site on my iPhone, both Safari and Chrome (everything is Safari on iPhone) the message wasn't there.

edit: I've tried .innerText instead of .innerHtml, still not working on Safari.

The reproduction of the problem below now uses set:html but my guess is it won't update the current day tomorrow, as with using the Component Script part. I left the client-side script that was not working on Safari commented out.

Link to Minimal Reproducible Example

https://github.com/aleksastojsic/astro-portfolio/blob/main/src/components/Footer.astro

Participation

  • I am willing to submit a pull request for this issue.
@rishi-raj-jain
Copy link
Contributor

Does it work in other browsers well? Would it be possible to share a deployed URL?

@aleksa-codes
Copy link
Contributor Author

aleksa-codes commented Sep 24, 2022

On desktop yes, I've tested it on all the browsers. I reverted the code from using the set:html to the <script> with .innerHtml now that was causing the issue, so it should be available on https://aleksa.codes at the bottom. Do you know if set:html with the function would also run only on build time as using the Component Script part (---) ?

edit: I don't own a Mac so can't test it on Safari there, only iPhone.

@rishi-raj-jain
Copy link
Contributor

@aleksastojsic Hey I just tried it and on desktop it fails to load on Safari

@rishi-raj-jain
Copy link
Contributor

rishi-raj-jain commented Sep 24, 2022

Looks more like a issue specific to Safari

@rishi-raj-jain
Copy link
Contributor

Screenshot 2022-09-24 at 4 41 56 PM

the error

@rishi-raj-jain
Copy link
Contributor

So what's happening is that define:vars produces the variable with let variable which is not supported with Safari: https://stackoverflow.com/questions/29194024/cant-use-let-keyword-in-safari-javascript

@rishi-raj-jain
Copy link
Contributor

Okay attempting a fix

@aleksa-codes
Copy link
Contributor Author

Great find. Not sure if related but TypeScript did throw me this:

Untitled

@aleksa-codes
Copy link
Contributor Author

aleksa-codes commented Sep 28, 2022

@rishi-raj-jain Hey I upgraded to [email protected] this morning and the message is still not there on Safari, also getting the "Could not find 'days'." as in the image above.

@rishi-raj-jain
Copy link
Contributor

@aleksastojsic So I get why this issue issue still exists. Now this is occuring because of the extra { } sent while using define:vars. Doing a quick PR for it.

@rishi-raj-jain
Copy link
Contributor

Though addition of those extra {} results in the error only in safari, and not on other browsers.

Simple reproduction:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      {
        const days = ["1"];
        function getDay() {
          return days[0];
        }
        console.log(getDay());
      }
    </script>
  </body>
</html>

Would throw console error in safari while work in chrome

@aleksa-codes
Copy link
Contributor Author

@rishi-raj-jain Should I open another issue? I feel like this is important to fix.

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

Successfully merging a pull request may close this issue.

2 participants