Skip to content

Remove target option #3674

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

Merged
merged 5 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
6 changes: 6 additions & 0 deletions .changeset/strong-parents-work.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'create-svelte': patch
'@sveltejs/kit': patch
---

Remove target option
7 changes: 1 addition & 6 deletions documentation/docs/14-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@ const config = {
register: true,
files: (filepath) => !/\.DS_STORE/.test(filepath)
},
target: null,
trailingSlash: 'never',
version: {
name: Date.now().toString(),
pollInterval: 0,
pollInterval: 0
},
vite: () => ({})
},
Expand Down Expand Up @@ -243,10 +242,6 @@ An object containing zero or more of the following values:
- `register` - if set to `false`, will disable automatic service worker registration
- `files` - a function with the type of `(filepath: string) => boolean`. When `true`, the given file will be available in `$service-worker.files`, otherwise it will be excluded.

### target

Specifies an element to mount the app to. It must be a DOM selector that identifies an element that exists in your template file. If unspecified, the app will be mounted to `document.body`.

### trailingSlash

Whether to remove, append, or ignore trailing slashes when resolving URLs to routes.
Expand Down
1 change: 0 additions & 1 deletion packages/adapter-cloudflare-workers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import adapter from '@sveltejs/adapter-cloudflare-workers';

export default {
kit: {
target: '#svelte',
adapter: adapter()
}
};
Expand Down
1 change: 0 additions & 1 deletion packages/adapter-cloudflare/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import adapter from '@sveltejs/adapter-cloudflare';

export default {
kit: {
target: '#svelte',
adapter: adapter()
}
};
Expand Down
3 changes: 1 addition & 2 deletions packages/adapter-netlify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ export default {
// if true, will split your app into multiple functions
// instead of creating a single one for the entire app
split: false
}),
target: '#svelte'
})
}
};
```
Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-static/test/apps/prerendered/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
%svelte.head%
</head>
<body>
<div id="svelte">%svelte.body%</div>
<div>%svelte.body%</div>
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import adapter from '../../../index.js';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter(),
target: '#svelte'
adapter: adapter()
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/adapter-static/test/apps/spa/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
%svelte.head%
</head>
<body>
<div id="svelte">%svelte.body%</div>
<div>%svelte.body%</div>
</body>
</html>
3 changes: 1 addition & 2 deletions packages/adapter-static/test/apps/spa/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ const config = {
kit: {
adapter: adapter({
fallback: '200.html'
}),
target: '#svelte'
})
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ const config = {
kit: {
adapter: adapter(),

// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte',

// Override http methods in the Todo forms
methodOverride: {
allowed: ['PATCH', 'DELETE']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ const config = {
kit: {
adapter: adapter(),

// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte',

// Override http methods in the Todo forms
methodOverride: {
allowed: ['PATCH', 'DELETE']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ const config = {
preprocess: preprocess(),

kit: {
adapter: adapter(),

// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte'
adapter: adapter()
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import adapter from '@sveltejs/adapter-auto';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter(),

// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte'
adapter: adapter()
}
};

Expand Down
2 changes: 1 addition & 1 deletion packages/create-svelte/templates/default/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
%svelte.head%
</head>
<body>
<div id="svelte">%svelte.body%</div>
<div>%svelte.body%</div>
</body>
</html>
3 changes: 0 additions & 3 deletions packages/create-svelte/templates/default/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@ const config = {
kit: {
adapter: adapter(),

// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte',

// Override http methods in the Todo forms
methodOverride: {
allowed: ['PATCH', 'DELETE']
Expand Down
2 changes: 1 addition & 1 deletion packages/create-svelte/templates/skeleton/src/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
%svelte.head%
</head>
<body>
<div id="svelte">%svelte.body%</div>
<div>%svelte.body%</div>
</body>
</html>
5 changes: 1 addition & 4 deletions packages/create-svelte/templates/skeleton/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import adapter from '@sveltejs/adapter-auto';
/** @type {import('@sveltejs/kit').Config} */
const config = {
kit: {
adapter: adapter(),

// hydrate the <div id="svelte"> element in src/app.html
target: '#svelte'
adapter: adapter()
}
};

Expand Down
1 change: 0 additions & 1 deletion packages/kit/src/core/build/build_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export class App {
root,
service_worker: ${has_service_worker ? "base + '/service-worker.js'" : 'null'},
router: ${s(config.kit.browser.router)},
target: ${s(config.kit.target)},
template,
template_contains_nonce: ${template.includes('%svelte.nonce%')},
trailing_slash: ${s(config.kit.trailingSlash)}
Expand Down
6 changes: 3 additions & 3 deletions packages/kit/src/core/config/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const get_defaults = (prefix = '') => ({
protocol: undefined,
router: undefined,
ssr: undefined,
target: null,
target: undefined,
trailingSlash: 'never',
version: {
name: Date.now().toString(),
Expand Down Expand Up @@ -125,10 +125,10 @@ test('errors on invalid values', () => {
validate_config({
kit: {
// @ts-expect-error - given value expected to throw
target: 42
appDir: 42
}
});
}, /^config\.kit\.target should be a string, if specified$/);
}, /^config\.kit\.appDir should be a string, if specified$/);
});

test('errors on invalid nested values', () => {
Expand Down
3 changes: 2 additions & 1 deletion packages/kit/src/core/config/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@ const options = object(
`${keypath} has been removed — use the handle hook instead: https://kit.svelte.dev/docs#hooks-handle'`
),

target: string(null),
// TODO remove this for 1.0
target: error((keypath) => `${keypath} is no longer required`),

trailingSlash: list(['never', 'always', 'ignore']),

Expand Down
1 change: 0 additions & 1 deletion packages/kit/src/core/dev/plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ export async function create_plugin(config, cwd) {
read: (file) => fs.readFileSync(path.join(config.kit.files.assets, file)),
root,
router: config.kit.browser.router,
target: config.kit.target,
template: ({ head, body, assets, nonce }) => {
return (
template
Expand Down
10 changes: 6 additions & 4 deletions packages/kit/src/runtime/server/page/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,13 @@ export async function render_response({
needs_nonce: options.template_contains_nonce
});

const target = hash(body);

// prettier-ignore
const init_app = `
import { start } from ${s(options.prefix + options.manifest._.entry.file)};
start({
target: ${options.target ? `document.querySelector(${s(options.target)})` : 'document.body'},
target: document.querySelector('[data-hydrate="${target}"]').parentNode,
paths: ${s(options.paths)},
session: ${try_serialize($session, (error) => {
throw new Error(`Failed to serialize session data: ${error.message}`);
Expand Down Expand Up @@ -234,15 +236,15 @@ export async function render_response({
.map((dep) => `\n\t<link rel="modulepreload" href="${options.prefix + dep}">`)
.join('');

const attributes = ['type="module"'];
const attributes = ['type="module"', `data-hydrate="${target}"`];

csp.add_script(init_app);

if (csp.script_needs_nonce) {
attributes.push(`nonce="${csp.nonce}"`);
}

head += `<script ${attributes.join(' ')}>${init_app}</script>`;
body += `\n\t\t<script ${attributes.join(' ')}>${init_app}</script>`;

// prettier-ignore
body += serialized_data
Expand All @@ -252,7 +254,7 @@ export async function render_response({

return `<script ${attributes}>${json}</script>`;
})
.join('\n\n\t');
.join('\n\t');
}

if (options.service_worker) {
Expand Down
2 changes: 1 addition & 1 deletion packages/kit/test/apps/options/source/template.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
</head>
<body>
<h1>I am in the template</h1>
<div id="content-goes-here">%svelte.body%</div>
<div>%svelte.body%</div>
</body>
</html>
1 change: 0 additions & 1 deletion packages/kit/test/apps/options/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ const config = {
},
appDir: '_wheee',
floc: true,
target: '#content-goes-here',
inlineStyleThreshold: 1024,
trailingSlash: 'always',
vite: {
Expand Down
1 change: 0 additions & 1 deletion packages/kit/types/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@ export interface Config {
register?: boolean;
files?: (filepath: string) => boolean;
};
target?: string;
trailingSlash?: TrailingSlash;
version?: {
name?: string;
Expand Down
1 change: 0 additions & 1 deletion packages/kit/types/internal.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ export interface SSRRenderOptions {
root: SSRComponent['default'];
router: boolean;
service_worker?: string;
target: string;
template({
head,
body,
Expand Down