Repository: sveltejs/svelte (JavaScript)

Issue:
Svelte 5: Using $page to set default value for component props
### Describe the bug

Newb here, apologies if I'm being stupid.

Not sure if this is a bug, but it surprised me and it used to work in Svelte 4 + SvelteKit.

```
  // $lib/components/SEO.svelte    
  let {
	location = $page.url.pathname,
	title = '',
  } = $props()
```

This generates the error when used in`+page.svelte`.

`ReferenceError: $page is not defined`

### Reproduction

I don't think there is a S5 + SK repl?

### Logs

```shell
ReferenceError: $page is not defined
    at Module.SEO [as default] (/Users/adam/src/adam.nz/src/lib/components/SEO.svelte:17:14)
    at Array._page (/Users/adam/src/adam.nz/src/routes/posts/[slug]/+page.svelte:25:31)
    at children (/Users/adam/src/adam.nz/.svelte-kit/generated/root.svelte:54:42)
    at Array._layout (/Users/adam/src/adam.nz/src/routes/+layout.svelte:27:12)
    at Root (/Users/adam/src/adam.nz/.svelte-kit/generated/root.svelte:45:40)
    at Module.render (/Users/adam/src/adam.nz/node_modules/.pnpm/svelte@5.0.0-next.222/node_modules/svelte/src/internal/server/index.js:109:2)
    at Function._render [as render] (/Users/adam/src/adam.nz/node_modules/.pnpm/svelte@5.0.0-next.222/node_modules/svelte/src/legacy/legacy-server.js:27:18)
    at Module.render_response (/Users/adam/src/adam.nz/node_modules/.pnpm/@sveltejs+kit@2.5.22_@sveltejs+vite-plugin-svelte@3.1.1_svelte@5.0.0-next.222_vite@5.4.1_@typ_rfye4o2ujzso4oc7ywriqbjr4y/node_modules/@sveltejs/kit/src/runtime/server/page/render.js:171:29)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Module.render_page (/Users/adam/src/adam.nz/node_modules/.pnpm/@sveltejs+kit@2.5.22_@sveltejs+vite-plugin-svelte@3.1.1_svelte@5.0.0-next.222_vite@5.4.1_@typ_rfye4o2ujzso4oc7ywriqbjr4y/node_modules/@sveltejs/kit/src/runtime/server/page/index.js:286:10)
```
```


### System Info

```shell
System:
    OS: macOS 14.5
    CPU: (8) arm64 Apple M2
    Memory: 153.58 MB / 16.00 GB
    Shell: 3.7.1 - /opt/homebrew/bin/fish
  Binaries:
    Node: 22.0.0 - /opt/homebrew/bin/node
    npm: 10.5.1 - /opt/homebrew/bin/npm
    pnpm: 9.1.1 - ~/Library/pnpm/pnpm
  Browsers:
    Brave Browser: 126.1.67.123
    Chrome: 127.0.6533.120
    Safari: 17.5
  npmPackages:
    svelte: 5.0.0-next.222 => 5.0.0-next.222
```
```


### Severity

annoyance


Discussion:
You can use https://sveltelab.dev/?t=next for the reproduction.

Silly question: did you import the page store?
Yes page store was imported. :-) 
> Yes page store was imported. :-)

If you can provide a reproduction I might be able to take a look 🤟🏻
@paoloricciuti thanks!  here's a repro.

https://www.sveltelab.dev/2mo78386mgqwf3w?files=.%2Fsrc%2Froutes%2F%2Bpage.svelte%2C.%2Fsrc%2Froutes%2FSEO.svelte
More simple [REPL](https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAAz1OywrCMBD8lWXxUEEoXksr-B3GQ2zTEsiLzbYiIf9u0oq3ncfOTMJZGxWxeyR00irs8B4CXpA_oYK4KcOq4OhXGivTx5F0YDDSLYNAjgJvwgnWNnhiSPAmzfJlFGSYyVsQv5A2siclUDiA0bvIEOSiYPg_NNdzDTq0BJs0a5VPuy3vF_kQm-Lq22NFbXZpd-Yy0vpJz1pN2DEV5pm_KJm3O98AAAA=) - set the code generation for server and there is no logic for getting `$page`.
> More simple [REPL](https://svelte-5-preview.vercel.app/#H4sIAAAAAAAAAz1OywrCMBD8lWXxUEEoXksr-B3GQ2zTEsiLzbYiIf9u0oq3ncfOTMJZGxWxeyR00irs8B4CXpA_oYK4KcOq4OhXGivTx5F0YDDSLYNAjgJvwgnWNnhiSPAmzfJlFGSYyVsQv5A2siclUDiA0bvIEOSiYPg_NNdzDTq0BJs0a5VPuy3vF_kQm-Lq22NFbXZpd-Yy0vpJz1pN2DEV5pm_KJm3O98AAAA=) - set the code generation for server and there is no logic for getting `$page`.

Great i tried that and forgot to move to server...let me see what's wrong.



Patch:
diff --git a/.changeset/smart-poets-care.md b/.changeset/smart-poets-care.md
new file mode 100644
index 000000000000..bb0dc071e3d4
--- /dev/null
+++ b/.changeset/smart-poets-care.md
@@ -0,0 +1,5 @@
+---
+'svelte': patch
+---
+
+fix: allow store as initial value for props in ssr
diff --git a/packages/svelte/src/compiler/phases/3-transform/server/visitors/VariableDeclaration.js b/packages/svelte/src/compiler/phases/3-transform/server/visitors/VariableDeclaration.js
index bd8a8f435e90..5e0b34a557c2 100644
--- a/packages/svelte/src/compiler/phases/3-transform/server/visitors/VariableDeclaration.js
+++ b/packages/svelte/src/compiler/phases/3-transform/server/visitors/VariableDeclaration.js
@@ -39,7 +39,9 @@ export function VariableDeclaration(node, context) {
 						}
 					}
 				});
-				declarations.push(b.declarator(id, b.id('$$props')));
+				declarations.push(
+					b.declarator(/** @type {Pattern} */ (context.visit(id)), b.id('$$props'))
+				);
 				continue;
 			}
 


Test patch:
diff --git a/packages/svelte/tests/server-side-rendering/samples/store-init-props/_config.js b/packages/svelte/tests/server-side-rendering/samples/store-init-props/_config.js
new file mode 100644
index 000000000000..f47bee71df87
--- /dev/null
+++ b/packages/svelte/tests/server-side-rendering/samples/store-init-props/_config.js
@@ -0,0 +1,3 @@
+import { test } from '../../test';
+
+export default test({});
diff --git a/packages/svelte/tests/server-side-rendering/samples/store-init-props/_expected.html b/packages/svelte/tests/server-side-rendering/samples/store-init-props/_expected.html
new file mode 100644
index 000000000000..56a6051ca2b0
--- /dev/null
+++ b/packages/svelte/tests/server-side-rendering/samples/store-init-props/_expected.html
@@ -0,0 +1,1 @@
+1
\ No newline at end of file
diff --git a/packages/svelte/tests/server-side-rendering/samples/store-init-props/main.svelte b/packages/svelte/tests/server-side-rendering/samples/store-init-props/main.svelte
new file mode 100644
index 000000000000..762a519e0378
--- /dev/null
+++ b/packages/svelte/tests/server-side-rendering/samples/store-init-props/main.svelte
@@ -0,0 +1,7 @@
+<script lang="ts">
+	import { writable } from 'svelte/store';
+	const page = writable(1);
+	const { value = $page } = $props();
+</script>
+
+{value}