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

Rendering content based on user's running OS fails, always returns null #7928

Closed
1 task done
jackf723 opened this issue Aug 2, 2023 · 1 comment
Closed
1 task done
Labels
needs triage Issue needs to be triaged

Comments

@jackf723
Copy link

jackf723 commented Aug 2, 2023

What version of astro are you using?

2.9.4

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

N/A

What package manager are you using?

NPM

What operating system are you using?

Windows 11

What browser are you using?

Firefox

Describe the Bug

I have an mdx page that is trying to render differing content based on whether the user is running Windows or Mac. However,determining platform via navigator seems to always return null, and thus present the I'm not sure what platform you're running text when viewing the html. Sample code below:

layout: ../../layouts/toc.astro
title: "document title"
author: "Insert author here"
date: "13 Jul 2023"

{() => {
const getOperatingSystem = (() => {
if (typeof window === "undefined") {
//If the code is not running in the browser (e.g., server-side), return null - we don't want errors
return null;
}
if (window.navigator.platform.indexOf("Win") != -1) {
return "Windows";
} else if (window.navigator.platform.indexOf("Mac") != -1) {
return "Mac";
} else {
return "Unknown";
}
})();

Your system should meet or exceed the following minimum requirements

if (getOperatingSystem === "Windows") { return ( <>

Windows Requirements:

); }

else if (getOperatingSystem=== "Mac") {
return (
<>

Mac Requirements:


</>
);
}

return ("you are currently running " + getOperatingSystem);

What's the expected result?

The Windows content would display on my Firefox browser running on Windows 11

Link to Minimal Reproducible Example

No link, code in bug description

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Aug 2, 2023
@Princesseuh
Copy link
Member

Your code snippet always run on the server. If you want it to run on the client, you'll need to put it in a script tag.

@Princesseuh Princesseuh closed this as not planned Won't fix, can't repro, duplicate, stale Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Issue needs to be triaged
Projects
None yet
Development

No branches or pull requests

2 participants