Skip to content

Commit

Permalink
chore: deprecated is_server! for G::IS_BROWSER
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Jan 2, 2022
1 parent 184381f commit 67e2d65
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
4 changes: 1 addition & 3 deletions docs/next/en-US/templates/intro.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,10 @@ There is one use-case though that requires a bit more fiddling: having a differe

## Checking Render Context

It's often necessary to make sure you're only running some logic on the client-side, particularly anything to do with `web_sys`, which will `panic!` if used on the server. Because Perseus renders your templates in both environments, you'll need to explicitly check if you want to do something only on the client (like get an authentication token from a cookie). This can be done trivially with the `is_server!` macro, which does exactly what it says on the tin. Here's an example from [here](https://github.com/arctic-hen7/perseus/blob/main/examples/i18n/src/templates/about.rs):
It's often necessary to make sure you're only running some logic on the client-side, particularly anything to do with `web_sys`, which will `panic!` if used on the server. Because Perseus renders your templates in both environments, you'll need to explicitly check if you want to do something only on the client (like get an authentication token from a cookie). This can be done trivially with Sycamore, just use `G::IS_BROWSER` (where `G` is the type parameter on your template). Here's an example from [here](https://github.com/arctic-hen7/perseus/blob/main/examples/i18n/src/templates/about.rs):

```rust
{{#include ../../../../examples/i18n/src/templates/about.rs}}
```

This is a very contrived example, but what you should note if you try this is the flash from `server` to `client` (when you go to the page from the URL bar, not when you go in from the link on the index page), because the page is pre-rendered on the server and then hydrated on the client. This is an important principle of Perseus, and you should be aware of this potential flashing (easily solved by a less contrived example) when your users [initially load](:advanced/initial-loads) a page.

One important thing to note with this macro is that it will only work in a _reactive scope_ because it uses Sycamore's [context system](https://sycamore-rs.netlify.app/docs/advanced/contexts). In other words, you can only use it inside a `view!`, `create_effect`, or the like.
4 changes: 2 additions & 2 deletions examples/i18n/src/templates/about.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use perseus::{is_server, t, Template};
use perseus::{t, Template};
use sycamore::prelude::{component, view, Html, View};

#[perseus::template(AboutPage)]
Expand All @@ -8,7 +8,7 @@ pub fn about_page() -> View<G> {
p { (t!("about")) }
p {
(
if is_server!() {
if !G::IS_BROWSER {
"This is running on the server."
} else {
"This is running on the client."
Expand Down
3 changes: 2 additions & 1 deletion packages/perseus/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,9 @@ pub type ArcTemplateMap<G> = HashMap<String, Arc<Template<G>>>;

/// Checks if we're on the server or the client. This must be run inside a reactive scope (e.g. a `template!` or `create_effect`),
/// because it uses Sycamore context.
// TODO use `Html` downcasting here
// TODO (0.4.0) Remove this altogether
#[macro_export]
#[deprecated(since = "0.3.1", note = "use `G::IS_BROWSER` instead")]
macro_rules! is_server {
() => {{
let render_ctx = ::sycamore::context::use_context::<::perseus::templates::RenderCtx>();
Expand Down

0 comments on commit 67e2d65

Please sign in to comment.