Repository: sveltejs/kit (js)

Issue:
Prerender crash with the new advanced layout feature
### Describe the bug

`vite build` crashes with an error related to a non existing path when the new advanced layout feature is used in the same app than a prerendered `+page.js`.

### Reproduction

https://stackblitz.com/edit/sveltejs-kit-template-default-tvrqmc?file=README.md

### Logs

```shell
Error: 404 /(example)/about
    at eval (file:///home/projects/sveltejs-kit-template-default-tvrqmc/node_modules/@sveltejs/kit/src/core/prerender/prerender.js:63:11)
    at save (file:///home/projects/sveltejs-kit-template-default-tvrqmc/node_modules/@sveltejs/kit/src/core/prerender/prerender.js:352:4)
    at visit (file:///home/projects/sveltejs-kit-template-default-tvrqmc/node_modules/@sveltejs/kit/src/core/prerender/prerender.js:239:3)
[vite-plugin-svelte-kit] Prerendering failed with code 1
error during build:
Error: Prerendering failed with code 1
    at ChildProcess.eval (file:///home/projects/sveltejs-kit-template-default-tvrqmc/node_modules/@sveltejs/kit/src/vite/index.js:445:14)
    at EventEmitter.emit (https://sveltejs-kit-template-default-tvrqmc.w.staticblitz.com/blitz.1aed30d7bfa712aa778f484223078bdc72898352.js:6:155574)
    at ChildProcess._handle.onexit (https://sveltejs-kit-template-default-tvrqmc.w.staticblitz.com/blitz.1aed30d7bfa712aa778f484223078bdc72898352.js:6:956472)
    at https://sveltejs-kit-template-default-tvrqmc.w.staticblitz.com/blitz.1aed30d7bfa712aa778f484223078bdc72898352.js:15:380472
    at _0x35818a (https://sveltejs-kit-template-default-tvrqmc.w.staticblitz.com/blitz.1aed30d7bfa712aa778f484223078bdc72898352.js:15:144412)
    at https://sveltejs-kit-template-default-tvrqmc.w.staticblitz.com/blitz.1aed30d7bfa712aa778f484223078bdc72898352.js:15:144195
```


### System Info

```shell
System:
    OS: Linux 5.19 Pop!_OS 22.04 LTS
    CPU: (8) x64 Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz
    Memory: 5.15 GB / 15.39 GB
    Container: Yes
    Shell: 5.8.1 - /usr/bin/zsh
  Binaries:
    Node: 16.16.0 - ~/.nvm/versions/node/v16.16.0/bin/node
    Yarn: 1.22.19 - ~/.nvm/versions/node/v16.16.0/bin/yarn
    npm: 8.11.0 - ~/.nvm/versions/node/v16.16.0/bin/npm
  Browsers:
    Brave Browser: 101.1.38.111
    Chromium: 104.0.5112.101
    Firefox: 103.0
```


### Severity

blocking an upgrade

### Additional Information

_No response_

Pull request:
prerender routes in a (group)
fixes #6230

### Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
- [x] It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
- [x] This message body should clearly illustrate what problems it solves.
- [x] Ideally, include a test that fails without this PR but passes with it.

### Tests
- [x] Run the tests with `pnpm test` and lint the project with `pnpm lint` and `pnpm check`

### Changesets
- [x] If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running `pnpm changeset` and following the prompts. All changesets should be `patch` until SvelteKit 1.0


Patch:
diff --git a/.changeset/afraid-games-jog.md b/.changeset/afraid-games-jog.md
new file mode 100644
index 000000000..18ae59c66
--- /dev/null
+++ b/.changeset/afraid-games-jog.md
@@ -0,0 +1,5 @@
+---
+'@sveltejs/kit': patch
+---
+
+[fix] prerender routes in a (group)
diff --git a/packages/kit/src/core/adapt/builder.js b/packages/kit/src/core/adapt/builder.js
index 03b7bb68a..e434d947f 100644
--- a/packages/kit/src/core/adapt/builder.js
+++ b/packages/kit/src/core/adapt/builder.js
@@ -5,6 +5,7 @@ import { pipeline } from 'stream';
 import { promisify } from 'util';
 import { copy, rimraf, mkdirp } from '../../utils/filesystem.js';
 import { generate_manifest } from '../generate_manifest/index.js';
+import { get_path } from '../../utils/routing.js';
 
 /**
  * Creates the Builder which is passed to adapters for building the application.
@@ -23,7 +24,7 @@ export function create_builder({ config, build_data, prerendered, log }) {
 	/** @param {import('types').RouteData} route */
 	// TODO routes should come pre-filtered
 	function not_prerendered(route) {
-		const path = route.page && !route.id.includes('[') && `/${route.id}`;
+		const path = route.page && get_path(route.id);
 		if (path) {
 			return !prerendered_paths.has(path) && !prerendered_paths.has(path + '/');
 		}
diff --git a/packages/kit/src/core/prerender/prerender.js b/packages/kit/src/core/prerender/prerender.js
index 410aa0e5b..985cf29f6 100644
--- a/packages/kit/src/core/prerender/prerender.js
+++ b/packages/kit/src/core/prerender/prerender.js
@@ -9,6 +9,8 @@ import { crawl } from './crawl.js';
 import { escape_html_attr } from '../../utils/escape.js';
 import { logger } from '../utils.js';
 import { load_config } from '../config/index.js';
+import { compact } from '../../utils/array.js';
+import { get_path } from '../../utils/routing.js';
 
 /**
  * @typedef {import('types').PrerenderErrorHandler} PrerenderErrorHandler
@@ -341,11 +343,10 @@ export async function prerender() {
 	if (config.prerender.enabled) {
 		for (const entry of config.prerender.entries) {
 			if (entry === '*') {
-				/** @type {import('types').ManifestData} */
-				const { routes } = (await import(pathToFileURL(manifest_path).href)).manifest._;
-				const entries = routes
-					.map((route) => (route.page && !route.id.includes('[') ? `/${route.id}` : ''))
-					.filter(Boolean);
+				/** @type {import('types').SSRManifest} */
+				const manifest = (await import(pathToFileURL(manifest_path).href)).manifest;
+				const { routes } = manifest._;
+				const entries = compact(routes.map((route) => route.page && get_path(route.id)));
 
 				for (const entry of entries) {
 					enqueue(null, config.paths.base + entry); // TODO can we pre-normalize these?
diff --git a/packages/kit/src/core/sync/create_manifest_data/index.js b/packages/kit/src/core/sync/create_manifest_data/index.js
index d39282fa4..906e0d9cd 100644
--- a/packages/kit/src/core/sync/create_manifest_data/index.js
+++ b/packages/kit/src/core/sync/create_manifest_data/index.js
@@ -82,6 +82,7 @@ function create_matchers(config, cwd) {
  * @param {string} fallback
  */
 function create_routes_and_nodes(cwd, config, fallback) {
+	/** @type {Map<string, import('types').RouteData>} */
 	const route_map = new Map();
 
 	/** @type {Map<string, import('./types').Part[][]>} */
diff --git a/packages/kit/src/utils/routing.js b/packages/kit/src/utils/routing.js
index 3ff8c9244..67c6e0c6a 100644
--- a/packages/kit/src/utils/routing.js
+++ b/packages/kit/src/utils/routing.js
@@ -96,6 +96,15 @@ export function affects_path(segment) {
 	return !/^\([^)]+\)$/.test(segment);
 }
 
+/**
+ * Turns a route ID into a path, if possible
+ * @param {string} id
+ */
+export function get_path(id) {
+	if (id.includes('[')) return null;
+	return `/${id.split('/').filter(affects_path).join('/')}`;
+}
+
 /**
  * @param {RegExpMatchArray} match
  * @param {string[]} names


Test patch:
diff --git a/packages/kit/test/prerendering/basics/src/routes/(grouped)/grouped/+page.svelte b/packages/kit/test/prerendering/basics/src/routes/(grouped)/grouped/+page.svelte
new file mode 100644
index 000000000..addccd133
--- /dev/null
+++ b/packages/kit/test/prerendering/basics/src/routes/(grouped)/grouped/+page.svelte
@@ -0,0 +1,1 @@
+<h1>grouped</h1>
diff --git a/packages/kit/test/prerendering/basics/test/test.js b/packages/kit/test/prerendering/basics/test/test.js
index b38c6ab65..2a450e965 100644
--- a/packages/kit/test/prerendering/basics/test/test.js
+++ b/packages/kit/test/prerendering/basics/test/test.js
@@ -205,4 +205,9 @@ test('$env - includes environment variables', () => {
 	assert.match(content, /.*PUBLIC_DYNAMIC: accessible anywhere\/evaluated at run time.*/gs);
 });
 
+test('prerenders a page in a (group)', () => {
+	const content = read('grouped.html');
+	assert.ok(content.includes('<h1>grouped</h1>'));
+});
+
 test.run();