Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/bug_report.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ body:
- http-guzzle
- inertia
- inertia-react
- inertia-svelte
- inertia-vue
- layout
- log
Expand Down
1 change: 1 addition & 0 deletions .github/ISSUE_TEMPLATE/feature_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ body:
- http-guzzle
- inertia
- inertia-react
- inertia-svelte
- inertia-vue
- layout
- log
Expand Down
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,10 @@
"type": "path",
"url": "packages/inertia-react"
},
{
"type": "path",
"url": "packages/inertia-svelte"
},
{
"type": "path",
"url": "packages/inertia-vue"
Expand Down Expand Up @@ -350,6 +354,7 @@
"marko/health": "self.version",
"marko/inertia": "self.version",
"marko/inertia-react": "self.version",
"marko/inertia-svelte": "self.version",
"marko/inertia-vue": "self.version",
"marko/http": "self.version",
"marko/layout": "self.version",
Expand Down Expand Up @@ -458,6 +463,7 @@
"Marko\\Health\\Tests\\": "packages/health/tests/",
"Marko\\Inertia\\Tests\\": "packages/inertia/tests/",
"Marko\\Inertia\\React\\Tests\\": "packages/inertia-react/tests/",
"Marko\\Inertia\\Svelte\\Tests\\": "packages/inertia-svelte/tests/",
"Marko\\Inertia\\Vue\\Tests\\": "packages/inertia-vue/tests/",
"Marko\\Http\\Tests\\": "packages/http/tests/",
"Marko\\Http\\Guzzle\\Tests\\": "packages/http-guzzle/tests/",
Expand Down
107 changes: 107 additions & 0 deletions docs/src/content/docs/packages/inertia-svelte.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
---
title: marko/inertia-svelte
description: Svelte companion for marko/inertia - configuration defaults and a frontend marker binding for Svelte.
---

Svelte companion for [`marko/inertia`](/docs/packages/inertia/) and [`marko/vite`](/docs/packages/vite/). It overlays the parent Inertia configuration with Svelte defaults and registers a frontend marker binding so installing multiple Inertia frontend companions fails loudly.

## Installation

```bash
composer require marko/inertia-svelte
```

Install the matching frontend dependencies in your app:

```bash
npm install @inertiajs/svelte svelte @sveltejs/vite-plugin-svelte vite
```

Refer to the [Inertia.js docs](https://inertiajs.com/) for currently supported versions of each frontend adapter.

## Configuration

This package contributes defaults to the parent `config/inertia.php` namespace:

```php title="packages/inertia-svelte/config/inertia.php"
return [
'assetEntry' => env('INERTIA_SVELTE_CLIENT_ENTRY', 'app/svelte-web/resources/js/app.js'),
];
```

| Key | Purpose |
| --- | --- |
| `assetEntry` | Vite entry used by browser-rendered Inertia responses. |

## Usage

Render Svelte-backed Inertia pages without passing an asset entry; `marko/inertia` reads it from configuration:

```php
use Marko\Inertia\Inertia;
use Marko\Routing\Http\Request;
use Marko\Routing\Http\Response;

class DashboardController
{
public function __construct(
private readonly Inertia $inertia,
) {}

public function index(Request $request): Response
{
return $this->inertia->render(
request: $request,
component: 'Dashboard',
);
}
}
```

Create the client entry at `app/svelte-web/resources/js/app.js`:

```js title="app/svelte-web/resources/js/app.js"
import { createInertiaApp } from '@inertiajs/svelte';
import { mount } from 'svelte';

createInertiaApp({
resolve: (name) => {
const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true });
return pages[`./Pages/${name}.svelte`];
},
setup({ el, App, props }) {
mount(App, { target: el, props });
},
});
```

Create the SSR entry at `app/svelte-web/resources/js/ssr.js`:

```js title="app/svelte-web/resources/js/ssr.js"
import { createInertiaApp } from '@inertiajs/svelte';
import createServer from '@inertiajs/svelte/server';
import { render } from 'svelte/server';

createServer((page) =>
createInertiaApp({
page,
resolve: (name) => {
const pages = import.meta.glob('./Pages/**/*.svelte', { eager: true });
return pages[`./Pages/${name}.svelte`];
},
setup({ App, props }) {
return render(App, { props });
},
}),
);
```

## API Reference

This package registers `Marko\Inertia\Frontend\InertiaFrontendInterface` to a Svelte marker implementation. Installing more than one Inertia frontend companion produces the same binding conflict protection used by Marko driver siblings.

## Related Packages

- [`marko/inertia`](/docs/packages/inertia/) - renders Inertia responses and handles SSR fallback
- [`marko/vite`](/docs/packages/vite/) - resolves the configured Svelte Vite entry
- [`marko/env`](/docs/packages/env/) - provides the `env()` helper used in `config/inertia.php`
5 changes: 5 additions & 0 deletions packages/inertia-svelte/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/tests export-ignore
/.github export-ignore
/.gitattributes export-ignore
/.gitignore export-ignore
/phpunit.xml.dist export-ignore
20 changes: 20 additions & 0 deletions packages/inertia-svelte/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
MIT License

Copyright (c) Devtomic LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
36 changes: 36 additions & 0 deletions packages/inertia-svelte/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# marko/inertia-svelte

Svelte companion for `marko/inertia` - configuration defaults and a frontend marker binding for Svelte.

## Installation

```bash
composer require marko/inertia-svelte
```

## Quick Example

```php
use Marko\Inertia\Inertia;
use Marko\Routing\Http\Request;
use Marko\Routing\Http\Response;

class DashboardController
{
public function __construct(
private readonly Inertia $inertia,
) {}

public function index(Request $request): Response
{
return $this->inertia->render(
request: $request,
component: 'Dashboard',
);
}
}
```

## Documentation

Full usage, API reference, and examples: [marko/inertia-svelte](https://marko.build/docs/packages/inertia-svelte/)
28 changes: 28 additions & 0 deletions packages/inertia-svelte/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "marko/inertia-svelte",
"description": "Svelte companion for marko/inertia - configuration and frontend marker binding",
"license": "MIT",
"type": "marko-module",
"require": {
"php": "^8.5",
"marko/core": "self.version",
"marko/env": "self.version",
"marko/inertia": "self.version",
"marko/vite": "self.version"
},
"autoload": {
"psr-4": {
"Marko\\Inertia\\Svelte\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Marko\\Inertia\\Svelte\\Tests\\": "tests/"
}
},
"extra": {
"marko": {
"module": true
}
}
}
7 changes: 7 additions & 0 deletions packages/inertia-svelte/config/inertia.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php

declare(strict_types=1);

return [
'assetEntry' => env('INERTIA_SVELTE_CLIENT_ENTRY', 'app/svelte-web/resources/js/app.js'),
];
12 changes: 12 additions & 0 deletions packages/inertia-svelte/module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

use Marko\Inertia\Frontend\InertiaFrontendInterface;
use Marko\Inertia\Svelte\SvelteInertiaFrontend;

return [
'bindings' => [
InertiaFrontendInterface::class => SvelteInertiaFrontend::class,
],
];
15 changes: 15 additions & 0 deletions packages/inertia-svelte/src/SvelteInertiaFrontend.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Marko\Inertia\Svelte;

use Marko\Inertia\Frontend\InertiaFrontendInterface;

class SvelteInertiaFrontend implements InertiaFrontendInterface
{
public function name(): string
{
return 'svelte';
}
}
36 changes: 36 additions & 0 deletions packages/inertia-svelte/tests/InertiaSvelteTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

use Marko\Inertia\Frontend\InertiaFrontendInterface;
use Marko\Inertia\Svelte\SvelteInertiaFrontend;

test('inertia-svelte config overlays the parent inertia config', function () {
$config = require dirname(__DIR__).'/config/inertia.php';

expect($config['assetEntry'])->toBe('app/svelte-web/resources/js/app.js');
expect($config)->not->toHaveKey('ssrEntry');
});

test('inertia-svelte binds the inertia frontend marker', function () {
$module = require dirname(__DIR__).'/module.php';

expect($module['bindings'])->toHaveKey(InertiaFrontendInterface::class)
->and($module['bindings'][InertiaFrontendInterface::class])->toBe(SvelteInertiaFrontend::class);
});

test('svelte inertia frontend identifies itself', function () {
expect((new SvelteInertiaFrontend())->name())->toBe('svelte');
});

test('inertia-svelte is a marko module', function () {
$composer = json_decode(
file_get_contents(dirname(__DIR__).'/composer.json'),
true,
flags: JSON_THROW_ON_ERROR,
);

expect(file_exists(dirname(__DIR__).'/module.php'))->toBeTrue()
->and($composer['extra']['marko']['module'])->toBeTrue()
->and($composer['autoload']['psr-4'])->toHaveKey('Marko\\Inertia\\Svelte\\');
});