-
Notifications
You must be signed in to change notification settings - Fork 27k
/
app_structure.rs
812 lines (745 loc) · 26.8 KB
/
app_structure.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
use std::collections::{BTreeMap, HashMap};
use anyhow::{bail, Result};
use indexmap::{indexmap, map::Entry, IndexMap};
use once_cell::sync::Lazy;
use regex::Regex;
use serde::{Deserialize, Serialize};
use turbo_tasks::{
debug::ValueDebugFormat, trace::TraceRawVcs, Completion, Completions, TaskInput, ValueToString,
Vc,
};
use turbopack_binding::{
turbo::tasks_fs::{DirectoryContent, DirectoryEntry, FileSystemEntryType, FileSystemPath},
turbopack::core::issue::{Issue, IssueExt, IssueSeverity},
};
use crate::next_config::NextConfig;
/// A final route in the app directory.
#[turbo_tasks::value]
#[derive(Default, Debug, Clone)]
pub struct Components {
#[serde(skip_serializing_if = "Option::is_none")]
pub page: Option<Vc<FileSystemPath>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub layout: Option<Vc<FileSystemPath>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub error: Option<Vc<FileSystemPath>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub loading: Option<Vc<FileSystemPath>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub template: Option<Vc<FileSystemPath>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub not_found: Option<Vc<FileSystemPath>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub default: Option<Vc<FileSystemPath>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub route: Option<Vc<FileSystemPath>>,
#[serde(skip_serializing_if = "Metadata::is_empty")]
pub metadata: Metadata,
}
impl Components {
fn without_leafs(&self) -> Self {
Self {
page: None,
layout: self.layout,
error: self.error,
loading: self.loading,
template: self.template,
not_found: self.not_found,
default: None,
route: None,
metadata: self.metadata.clone(),
}
}
fn merge(a: &Self, b: &Self) -> Self {
Self {
page: a.page.or(b.page),
layout: a.layout.or(b.layout),
error: a.error.or(b.error),
loading: a.loading.or(b.loading),
template: a.template.or(b.template),
not_found: a.not_found.or(b.not_found),
default: a.default.or(b.default),
route: a.route.or(b.route),
metadata: Metadata::merge(&a.metadata, &b.metadata),
}
}
}
#[turbo_tasks::value_impl]
impl Components {
/// Returns a completion that changes when any route in the components
/// changes.
#[turbo_tasks::function]
pub async fn routes_changed(self: Vc<Self>) -> Result<Vc<Completion>> {
self.await?;
Ok(Completion::new())
}
}
/// A single metadata file plus an optional "alt" text file.
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, TraceRawVcs)]
pub enum MetadataWithAltItem {
Static {
path: Vc<FileSystemPath>,
alt_path: Option<Vc<FileSystemPath>>,
},
Dynamic {
path: Vc<FileSystemPath>,
},
}
/// A single metadata file.
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, TaskInput, TraceRawVcs)]
pub enum MetadataItem {
Static { path: Vc<FileSystemPath> },
Dynamic { path: Vc<FileSystemPath> },
}
/// Metadata file that can be placed in any segment of the app directory.
#[derive(Default, Clone, Debug, Serialize, Deserialize, PartialEq, Eq, TraceRawVcs)]
pub struct Metadata {
#[serde(skip_serializing_if = "Vec::is_empty")]
pub icon: Vec<MetadataWithAltItem>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub apple: Vec<MetadataWithAltItem>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub twitter: Vec<MetadataWithAltItem>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub open_graph: Vec<MetadataWithAltItem>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub favicon: Vec<MetadataWithAltItem>,
#[serde(skip_serializing_if = "Option::is_none")]
pub manifest: Option<MetadataItem>,
}
impl Metadata {
pub fn is_empty(&self) -> bool {
let Metadata {
icon,
apple,
twitter,
open_graph,
favicon,
manifest,
} = self;
icon.is_empty()
&& apple.is_empty()
&& twitter.is_empty()
&& open_graph.is_empty()
&& favicon.is_empty()
&& manifest.is_none()
}
fn merge(a: &Self, b: &Self) -> Self {
Self {
icon: a.icon.iter().chain(b.icon.iter()).copied().collect(),
apple: a.apple.iter().chain(b.apple.iter()).copied().collect(),
twitter: a.twitter.iter().chain(b.twitter.iter()).copied().collect(),
open_graph: a
.open_graph
.iter()
.chain(b.open_graph.iter())
.copied()
.collect(),
favicon: a.favicon.iter().chain(b.favicon.iter()).copied().collect(),
manifest: a.manifest.or(b.manifest),
}
}
}
/// Metadata files that can be placed in the root of the app directory.
#[turbo_tasks::value]
#[derive(Default, Clone, Debug)]
pub struct GlobalMetadata {
#[serde(skip_serializing_if = "Option::is_none")]
pub favicon: Option<MetadataItem>,
#[serde(skip_serializing_if = "Option::is_none")]
pub robots: Option<MetadataItem>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sitemap: Option<MetadataItem>,
}
impl GlobalMetadata {
pub fn is_empty(&self) -> bool {
let GlobalMetadata {
favicon,
robots,
sitemap,
} = self;
favicon.is_none() && robots.is_none() && sitemap.is_none()
}
}
#[turbo_tasks::value]
#[derive(Debug)]
pub struct DirectoryTree {
/// key is e.g. "dashboard", "(dashboard)", "@slot"
pub subdirectories: BTreeMap<String, Vc<DirectoryTree>>,
pub components: Vc<Components>,
}
#[turbo_tasks::value_impl]
impl DirectoryTree {
/// Returns a completion that changes when any route in the whole tree
/// changes.
#[turbo_tasks::function]
pub async fn routes_changed(self: Vc<Self>) -> Result<Vc<Completion>> {
let DirectoryTree {
subdirectories,
components,
} = &*self.await?;
let mut children = Vec::new();
children.push(components.routes_changed());
for child in subdirectories.values() {
children.push(child.routes_changed());
}
Ok(Vc::<Completions>::cell(children).completed())
}
}
#[turbo_tasks::value(transparent)]
pub struct OptionAppDir(Option<Vc<FileSystemPath>>);
#[turbo_tasks::value_impl]
impl OptionAppDir {
/// Returns a completion that changes when any route in the whole tree
/// changes.
#[turbo_tasks::function]
pub async fn routes_changed(
self: Vc<Self>,
next_config: Vc<NextConfig>,
) -> Result<Vc<Completion>> {
if let Some(app_dir) = *self.await? {
let directory_tree = get_directory_tree(app_dir, next_config.page_extensions());
directory_tree.routes_changed().await?;
}
Ok(Completion::new())
}
}
/// Finds and returns the [DirectoryTree] of the app directory if existing.
#[turbo_tasks::function]
pub async fn find_app_dir(project_path: Vc<FileSystemPath>) -> Result<Vc<OptionAppDir>> {
let app = project_path.join("app".to_string());
let src_app = project_path.join("src/app".to_string());
let app_dir = if *app.get_type().await? == FileSystemEntryType::Directory {
app
} else if *src_app.get_type().await? == FileSystemEntryType::Directory {
src_app
} else {
return Ok(Vc::cell(None));
}
.resolve()
.await?;
Ok(Vc::cell(Some(app_dir)))
}
/// Finds and returns the [DirectoryTree] of the app directory if enabled and
/// existing.
#[turbo_tasks::function]
pub async fn find_app_dir_if_enabled(
project_path: Vc<FileSystemPath>,
next_config: Vc<NextConfig>,
) -> Result<Vc<OptionAppDir>> {
if !*next_config.app_dir().await? {
return Ok(Vc::cell(None));
}
Ok(find_app_dir(project_path))
}
static STATIC_LOCAL_METADATA: Lazy<HashMap<&'static str, &'static [&'static str]>> =
Lazy::new(|| {
HashMap::from([
(
"icon",
&["ico", "jpg", "jpeg", "png", "svg"] as &'static [&'static str],
),
("apple-icon", &["jpg", "jpeg", "png"]),
("opengraph-image", &["jpg", "jpeg", "png", "gif"]),
("twitter-image", &["jpg", "jpeg", "png", "gif"]),
("favicon", &["ico"]),
("manifest", &["webmanifest", "json"]),
])
});
static STATIC_GLOBAL_METADATA: Lazy<HashMap<&'static str, &'static [&'static str]>> =
Lazy::new(|| {
HashMap::from([
("favicon", &["ico"] as &'static [&'static str]),
("robots", &["txt"]),
("sitemap", &["xml"]),
])
});
fn match_metadata_file<'a>(
basename: &'a str,
page_extensions: &[String],
) -> Option<(&'a str, i32, bool)> {
let (stem, ext) = basename.split_once('.')?;
static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new("^(.*?)(\\d*)$").unwrap());
let captures = REGEX.captures(stem).expect("the regex will always match");
let stem = captures.get(1).unwrap().as_str();
let num: i32 = captures.get(2).unwrap().as_str().parse().unwrap_or(-1);
if page_extensions.iter().any(|e| e == ext) {
return Some((stem, num, true));
}
let exts = STATIC_LOCAL_METADATA.get(stem)?;
exts.contains(&ext).then_some((stem, num, false))
}
#[turbo_tasks::function]
async fn get_directory_tree(
dir: Vc<FileSystemPath>,
page_extensions: Vc<Vec<String>>,
) -> Result<Vc<DirectoryTree>> {
let DirectoryContent::Entries(entries) = &*dir.read_dir().await? else {
bail!("{} must be a directory", dir.to_string().await?);
};
let page_extensions_value = page_extensions.await?;
let mut subdirectories = BTreeMap::new();
let mut components = Components::default();
let mut metadata_icon = Vec::new();
let mut metadata_apple = Vec::new();
let mut metadata_open_graph = Vec::new();
let mut metadata_twitter = Vec::new();
let mut metadata_favicon = Vec::new();
for (basename, entry) in entries {
match *entry {
DirectoryEntry::File(file) => {
if let Some((stem, ext)) = basename.split_once('.') {
if page_extensions_value.iter().any(|e| e == ext) {
match stem {
"page" => components.page = Some(file),
"layout" => components.layout = Some(file),
"error" => components.error = Some(file),
"loading" => components.loading = Some(file),
"template" => components.template = Some(file),
"not-found" => components.not_found = Some(file),
"default" => components.default = Some(file),
"route" => components.route = Some(file),
"manifest" => {
components.metadata.manifest =
Some(MetadataItem::Dynamic { path: file });
continue;
}
_ => {}
}
}
}
if let Some((metadata_type, num, dynamic)) =
match_metadata_file(basename.as_str(), &page_extensions_value)
{
if metadata_type == "manifest" {
if num == -1 {
components.metadata.manifest =
Some(MetadataItem::Static { path: file });
}
continue;
}
let entry = match metadata_type {
"icon" => Some(&mut metadata_icon),
"apple-icon" => Some(&mut metadata_apple),
"twitter-image" => Some(&mut metadata_twitter),
"opengraph-image" => Some(&mut metadata_open_graph),
"favicon" => Some(&mut metadata_favicon),
_ => None,
};
if let Some(entry) = entry {
if dynamic {
entry.push((num, MetadataWithAltItem::Dynamic { path: file }));
} else {
let file_value = file.await?;
let file_name = file_value.file_name();
let basename = file_name
.rsplit_once('.')
.map_or(file_name, |(basename, _)| basename);
let alt_path = file.parent().join(format!("{}.alt.txt", basename));
let alt_path =
matches!(&*alt_path.get_type().await?, FileSystemEntryType::File)
.then_some(alt_path);
entry.push((
num,
MetadataWithAltItem::Static {
path: file,
alt_path,
},
));
}
}
}
}
DirectoryEntry::Directory(dir) => {
// appDir ignores paths starting with an underscore
if !basename.starts_with('_') {
let result = get_directory_tree(dir, page_extensions);
subdirectories.insert(get_underscore_normalized_path(basename), result);
}
}
// TODO(WEB-952) handle symlinks in app dir
_ => {}
}
}
fn sort<T>(mut list: Vec<(i32, T)>) -> Vec<T> {
list.sort_by_key(|(num, _)| *num);
list.into_iter().map(|(_, item)| item).collect()
}
components.metadata.icon = sort(metadata_icon);
components.metadata.apple = sort(metadata_apple);
components.metadata.twitter = sort(metadata_twitter);
components.metadata.open_graph = sort(metadata_open_graph);
components.metadata.favicon = sort(metadata_favicon);
Ok(DirectoryTree {
subdirectories,
components: components.cell(),
}
.cell())
}
#[turbo_tasks::value]
#[derive(Debug, Clone)]
pub struct LoaderTree {
pub segment: String,
pub parallel_routes: IndexMap<String, Vc<LoaderTree>>,
pub components: Vc<Components>,
}
#[turbo_tasks::function]
async fn merge_loader_trees(
app_dir: Vc<FileSystemPath>,
tree1: Vc<LoaderTree>,
tree2: Vc<LoaderTree>,
) -> Result<Vc<LoaderTree>> {
let tree1 = tree1.await?;
let tree2 = tree2.await?;
let segment = if !tree1.segment.is_empty() {
tree1.segment.to_string()
} else {
tree2.segment.to_string()
};
let mut parallel_routes = tree1.parallel_routes.clone();
for (key, &tree2_route) in tree2.parallel_routes.iter() {
add_parallel_route(app_dir, &mut parallel_routes, key.clone(), tree2_route).await?
}
let components = Components::merge(&*tree1.components.await?, &*tree2.components.await?).cell();
Ok(LoaderTree {
segment,
parallel_routes,
components,
}
.cell())
}
#[derive(
Copy,
Clone,
PartialEq,
Eq,
Serialize,
Deserialize,
TraceRawVcs,
ValueDebugFormat,
Debug,
TaskInput,
)]
pub enum Entrypoint {
AppPage { loader_tree: Vc<LoaderTree> },
AppRoute { path: Vc<FileSystemPath> },
}
#[turbo_tasks::value(transparent)]
pub struct Entrypoints(IndexMap<String, Entrypoint>);
fn is_parallel_route(name: &str) -> bool {
name.starts_with('@')
}
fn match_parallel_route(name: &str) -> Option<&str> {
name.strip_prefix('@')
}
async fn add_parallel_route(
app_dir: Vc<FileSystemPath>,
result: &mut IndexMap<String, Vc<LoaderTree>>,
key: String,
loader_tree: Vc<LoaderTree>,
) -> Result<()> {
match result.entry(key) {
Entry::Occupied(mut e) => {
let value = e.get_mut();
*value = merge_loader_trees(app_dir, *value, loader_tree)
.resolve()
.await?;
}
Entry::Vacant(e) => {
e.insert(loader_tree);
}
}
Ok(())
}
async fn add_app_page(
app_dir: Vc<FileSystemPath>,
result: &mut IndexMap<String, Entrypoint>,
key: String,
loader_tree: Vc<LoaderTree>,
) -> Result<()> {
match result.entry(key) {
Entry::Occupied(mut e) => {
let value = e.get_mut();
let Entrypoint::AppPage { loader_tree: value } = value else {
DirectoryTreeIssue {
app_dir,
message: Vc::cell(format!("Conflicting route at {}", e.key())),
severity: IssueSeverity::Error.cell(),
}
.cell()
.emit();
return Ok(());
};
*value = merge_loader_trees(app_dir, *value, loader_tree)
.resolve()
.await?;
}
Entry::Vacant(e) => {
e.insert(Entrypoint::AppPage { loader_tree });
}
}
Ok(())
}
async fn add_app_route(
app_dir: Vc<FileSystemPath>,
result: &mut IndexMap<String, Entrypoint>,
key: String,
path: Vc<FileSystemPath>,
) -> Result<()> {
match result.entry(key) {
Entry::Occupied(mut e) => {
DirectoryTreeIssue {
app_dir,
message: Vc::cell(format!("Conflicting route at {}", e.key())),
severity: IssueSeverity::Error.cell(),
}
.cell()
.emit();
*e.get_mut() = Entrypoint::AppRoute { path };
}
Entry::Vacant(e) => {
e.insert(Entrypoint::AppRoute { path });
}
}
Ok(())
}
#[turbo_tasks::function]
pub fn get_entrypoints(
app_dir: Vc<FileSystemPath>,
page_extensions: Vc<Vec<String>>,
) -> Vc<Entrypoints> {
directory_tree_to_entrypoints(app_dir, get_directory_tree(app_dir, page_extensions))
}
#[turbo_tasks::function]
fn directory_tree_to_entrypoints(
app_dir: Vc<FileSystemPath>,
directory_tree: Vc<DirectoryTree>,
) -> Vc<Entrypoints> {
directory_tree_to_entrypoints_internal(app_dir, "".to_string(), directory_tree, "/".to_string())
}
#[turbo_tasks::function]
async fn directory_tree_to_entrypoints_internal(
app_dir: Vc<FileSystemPath>,
directory_name: String,
directory_tree: Vc<DirectoryTree>,
path_prefix: String,
) -> Result<Vc<Entrypoints>> {
let mut result = IndexMap::new();
let directory_tree = &*directory_tree.await?;
let subdirectories = &directory_tree.subdirectories;
let components = directory_tree.components.await?;
let current_level_is_parallel_route = is_parallel_route(&directory_name);
if let Some(page) = components.page {
add_app_page(
app_dir,
&mut result,
path_prefix.to_string(),
if current_level_is_parallel_route {
LoaderTree {
segment: "__PAGE__".to_string(),
parallel_routes: IndexMap::new(),
components: Components {
page: Some(page),
..Default::default()
}
.cell(),
}
.cell()
} else {
LoaderTree {
segment: directory_name.to_string(),
parallel_routes: indexmap! {
"children".to_string() => LoaderTree {
segment: "__PAGE__".to_string(),
parallel_routes: IndexMap::new(),
components: Components {
page: Some(page),
..Default::default()
}
.cell(),
}
.cell(),
},
components: components.without_leafs().cell(),
}
.cell()
},
)
.await?;
}
if let Some(default) = components.default {
add_app_page(
app_dir,
&mut result,
path_prefix.to_string(),
if current_level_is_parallel_route {
LoaderTree {
segment: "__DEFAULT__".to_string(),
parallel_routes: IndexMap::new(),
components: Components {
default: Some(default),
..Default::default()
}
.cell(),
}
.cell()
} else {
LoaderTree {
segment: directory_name.to_string(),
parallel_routes: indexmap! {
"children".to_string() => LoaderTree {
segment: "__DEFAULT__".to_string(),
parallel_routes: IndexMap::new(),
components: Components {
default: Some(default),
..Default::default()
}
.cell(),
}
.cell(),
},
components: components.without_leafs().cell(),
}
.cell()
},
)
.await?;
}
if let Some(route) = components.route {
add_app_route(app_dir, &mut result, path_prefix.to_string(), route).await?;
}
if path_prefix == "/" {
// Next.js has this logic in "collect-app-paths", where the root not-found page
// is considered as its own entry point.
if let Some(_not_found) = components.not_found {
add_app_page(
app_dir,
&mut result,
"/_not-found".to_string(),
LoaderTree {
segment: directory_name.to_string(),
parallel_routes: indexmap! {
// TODO(alexkirsz) Next.js has a __DEFAULT__ entry for
// next/dist/client/components/parallel-route-default
// here.
},
components: components.without_leafs().cell(),
}
.cell(),
)
.await?;
}
}
for (subdir_name, &subdirectory) in subdirectories.iter() {
let parallel_route_key = match_parallel_route(subdir_name);
let map = directory_tree_to_entrypoints_internal(
app_dir,
subdir_name.to_string(),
subdirectory,
// TODO(alexkirsz) We don't check optional segment here because Next.js seems to expect
// it, although this might just need to be computed as "original name".
if parallel_route_key.is_some() {
path_prefix.clone()
} else if path_prefix == "/" {
format!("/{subdir_name}")
} else {
format!("{path_prefix}/{subdir_name}")
},
)
.await?;
for (full_path, &entrypoint) in map.iter() {
match entrypoint {
Entrypoint::AppPage { loader_tree } => {
if current_level_is_parallel_route {
add_app_page(app_dir, &mut result, full_path.clone(), loader_tree).await?;
} else {
let key = parallel_route_key.unwrap_or("children").to_string();
let child_loader_tree = LoaderTree {
segment: directory_name.to_string(),
parallel_routes: indexmap! {
key => loader_tree,
},
components: components.without_leafs().cell(),
}
.cell();
add_app_page(app_dir, &mut result, full_path.clone(), child_loader_tree)
.await?;
}
}
Entrypoint::AppRoute { path } => {
add_app_route(app_dir, &mut result, full_path.clone(), path).await?;
}
}
}
}
Ok(Vc::cell(result))
}
/// ref: https://github.com/vercel/next.js/blob/c390c1662bc79e12cf7c037dcb382ef5ead6e492/packages/next/src/build/entries.ts#L119
/// if path contains %5F, replace it with _.
fn get_underscore_normalized_path(path: &str) -> String {
path.replace("%5F", "_")
}
/// Returns the global metadata for an app directory.
#[turbo_tasks::function]
pub async fn get_global_metadata(
app_dir: Vc<FileSystemPath>,
page_extensions: Vc<Vec<String>>,
) -> Result<Vc<GlobalMetadata>> {
let DirectoryContent::Entries(entries) = &*app_dir.read_dir().await? else {
bail!("app_dir must be a directory")
};
let mut metadata = GlobalMetadata::default();
for (basename, entry) in entries {
if let DirectoryEntry::File(file) = *entry {
if let Some((stem, ext)) = basename.split_once('.') {
let list = match stem {
"favicon" => Some(&mut metadata.favicon),
"sitemap" => Some(&mut metadata.sitemap),
"robots" => Some(&mut metadata.robots),
_ => None,
};
if let Some(list) = list {
if page_extensions.await?.iter().any(|e| e == ext) {
*list = Some(MetadataItem::Dynamic { path: file });
}
if STATIC_GLOBAL_METADATA.get(stem).unwrap().contains(&ext) {
*list = Some(MetadataItem::Static { path: file });
}
}
}
}
// TODO(WEB-952) handle symlinks in app dir
}
Ok(metadata.cell())
}
#[turbo_tasks::value(shared)]
struct DirectoryTreeIssue {
pub severity: Vc<IssueSeverity>,
pub app_dir: Vc<FileSystemPath>,
pub message: Vc<String>,
}
#[turbo_tasks::value_impl]
impl Issue for DirectoryTreeIssue {
#[turbo_tasks::function]
fn severity(&self) -> Vc<IssueSeverity> {
self.severity
}
#[turbo_tasks::function]
async fn title(&self) -> Result<Vc<String>> {
Ok(Vc::cell(
"An issue occurred while preparing your Next.js app".to_string(),
))
}
#[turbo_tasks::function]
fn category(&self) -> Vc<String> {
Vc::cell("next app".to_string())
}
#[turbo_tasks::function]
fn context(&self) -> Vc<FileSystemPath> {
self.app_dir
}
#[turbo_tasks::function]
fn description(&self) -> Vc<String> {
self.message
}
}