Skip to content

Commit

Permalink
Add option to disable ssr globally in config
Browse files Browse the repository at this point in the history
  • Loading branch information
woutdp committed Sep 25, 2024
1 parent 0bcbbe0 commit 0376277
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## UNRELEASED

## [0.14.0] - 2024-09-25

### Added

- Added the option to disable SSR through a config variable [Issue 144](https://github.com/woutdp/live_svelte/issues/144)

### Changed

- Updated :nodejs to `3.1`
Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,21 @@ In theses cases you can turn off SSR.

#### Disabling SSR

SSR is enabled by default when you install LiveSvelte. If you don't want to use Server-Side Rendering for Svelte, you have 2 options:
SSR is enabled by default when you install LiveSvelte. If you don't want to use Server-Side Rendering for Svelte, you can disable it in the following ways:

##### Globally

If you don't want to use SSR on any component you can disable it globally.
This will automatically be the case if you don't include the `NodeJS` supervisor in the `application.ex` file

There are 2 ways of doing this
- Don't include the `NodeJS` supervisor in the `application.ex` file
or
- Add `ssr: false` to the `live_svelte` config in your `config.exs` file like so:

```elixir
config :live_svelte,
ssr: false
```

##### Component

Expand Down
3 changes: 2 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Config

config :live_svelte,
ssr_module: LiveSvelte.SSR.NodeJS
ssr_module: LiveSvelte.SSR.NodeJS,
ssr: true

if Mix.env() == :dev do
esbuild = fn args ->
Expand Down
4 changes: 4 additions & 0 deletions example_project/config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ config :logger, :console,
# Use Jason for JSON parsing in Phoenix
config :phoenix, :json_library, Jason

# Configure live_svelte
config :live_svelte,
ssr: true

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs"
3 changes: 2 additions & 1 deletion lib/component.ex
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ defmodule LiveSvelte do
def svelte(assigns) do
init = assigns.__changed__ == nil
dead = assigns.socket == nil or not LiveView.connected?(assigns.socket)
ssr_active = Application.get_env(:live_svelte, :ssr, true)

slots =
assigns
|> Slots.rendered_slot_map()
|> Slots.js_process()

ssr_code =
if init and dead and assigns.ssr do
if init and dead and ssr_active and assigns.ssr do
try do
props =
Map.merge(
Expand Down

0 comments on commit 0376277

Please sign in to comment.