Skip to content

Commit e5c7036

Browse files
authored
JS file collocation with Razor components (#23327)
1 parent fe9bacf commit e5c7036

File tree

3 files changed

+66
-6
lines changed

3 files changed

+66
-6
lines changed

aspnetcore/blazor/components/class-libraries.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ The distinction between using the `Link` component in a child component and plac
252252
* Can be modified by application state. A hard-coded `<link>` HTML tag can't be modified by application state.
253253
* Is removed from the HTML `<head>` when the parent component is no longer rendered.
254254

255-
## Create an RCL with static assets
255+
## Create an RCL with static assets in the `wwwroot` folder
256256

257257
An RCL's static assets are available to any app that consumes the library.
258258

@@ -311,6 +311,10 @@ Rendered `Jeep` component:
311311

312312
For more information, see <xref:razor-pages/ui-class#create-an-rcl-with-static-assets>.
313313

314+
## Create an RCL with JavaScript files collocated with components
315+
316+
[!INCLUDE[](~/includes/js-collocation.md)]
317+
314318
## Supply components and static assets to multiple hosted Blazor apps
315319

316320
For more information, see <xref:blazor/host-and-deploy/webassembly#static-assets-and-class-libraries-for-multiple-blazor-webassembly-apps>.

aspnetcore/blazor/javascript-interoperability/index.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ Load JavaScript (JS) code using any of the following approaches:
4040

4141
* [Load a script in `<head>` markup](#load-a-script-in-head-markup) (*Not generally recommended*)
4242
* [Load a script in `<body>` markup](#load-a-script-in-body-markup)
43-
* [Load a script from an external JS file (`.js`)](#load-a-script-from-an-external-js-file-js)
43+
* [Load a script from an external JavaScript file (`.js`) collocated with a component](#load-a-script-from-an-external-javascript-file-js-collocated-with-a-component)
44+
* [Load a script from an external JavaScript file (`.js`)](#load-a-script-from-an-external-javascript-file-js)
4445
* [Inject a script after Blazor starts](#inject-a-script-after-blazor-starts)
4546

4647
> [!WARNING]
@@ -53,7 +54,7 @@ Load JavaScript (JS) code using any of the following approaches:
5354

5455
*The approach in this section isn't generally recommended.*
5556

56-
Place the script (`<script>...</script>`) in the `<head>` element markup of `wwwroot/index.html` (Blazor WebAssembly) or `Pages/_Layout.cshtml` (Blazor Server):
57+
Place the JavaScript (JS) tags (`<script>...</script>`) in the `<head>` element markup of `wwwroot/index.html` (Blazor WebAssembly) or `Pages/_Layout.cshtml` (Blazor Server):
5758

5859
```html
5960
<head>
@@ -74,7 +75,7 @@ Loading JS from the `<head>` isn't the best approach for the following reasons:
7475

7576
### Load a script in `<body>` markup
7677

77-
Place the script (`<script>...</script>`) inside the closing `</body>` element markup of `wwwroot/index.html` (Blazor WebAssembly) or `Pages/_Layout.cshtml` (Blazor Server):
78+
Place the JavaScript (JS) tags (`<script>...</script>`) inside the closing `</body>` element markup of `wwwroot/index.html` (Blazor WebAssembly) or `Pages/_Layout.cshtml` (Blazor Server):
7879

7980
```html
8081
<body>
@@ -91,9 +92,15 @@ Place the script (`<script>...</script>`) inside the closing `</body>` element
9192

9293
The `{webassembly|server}` placeholder in the preceding markup is either `webassembly` for a Blazor WebAssembly app (`blazor.webassembly.js`) or `server` for a Blazor Server app (`blazor.server.js`).
9394

94-
### Load a script from an external JS file (`.js`)
95+
### Load a script from an external JavaScript file (`.js`) collocated with a component
9596

96-
Place the script (`<script>...</script>`) with a script `src` path inside the closing `</body>` tag after the Blazor script reference.
97+
[!INCLUDE[](~/includes/js-collocation.md)]
98+
99+
For more information on RCLs, see <xref:blazor/components/class-libraries>.
100+
101+
### Load a script from an external JavaScript file (`.js`)
102+
103+
Place the JavaScript (JS) tags (`<script>...</script>`) with a script source (`src`) path inside the closing `</body>` tag after the Blazor script reference.
97104

98105
In `wwwroot/index.html` (Blazor WebAssembly) or `Pages/_Layout.cshtml` (Blazor Server):
99106

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
no-loc: [Home, Privacy, Kestrel, appsettings.json, "ASP.NET Core Identity", cookie, Cookie, Blazor, "Blazor Server", "Blazor WebAssembly", "Identity", "Let's Encrypt", Razor, SignalR]
3+
---
4+
Collocation of JavaScript (JS) files for pages, views, and Razor components is a convenient way to organize scripts in an app.
5+
6+
Collocate JS files using the following filename extension conventions:
7+
8+
* Pages of Razor Pages apps and views of MVC apps: `.cshtml.js`. Examples:
9+
* `Pages/Contact.cshtml.js` for the `Contact` page of a Razor Pages app at `Pages/Contact.cshtml`.
10+
* `Views/Home/Contact.cshtml.js` for the `Contact` view of an MVC app at `Views/Home/Contact.cshtml`.
11+
* Razor components of Blazor apps: `.razor.js`. Example: `Pages/Index.razor.js` for the `Index` component at `Pages/Index.razor`.
12+
13+
Collocated JS files are publicly addressable using the path to the file in the project:
14+
15+
* Pages, views, and components from a collocated scripts file in the app:
16+
17+
`{PATH}/{PAGE, VIEW, OR COMPONENT}.{EXTENSION}`
18+
19+
* The `{PATH}` placeholder is the path to the page, view, or component.
20+
* The `{PAGE, VIEW, OR COMPONENT}` placeholder is the page, view, or component.
21+
* The `{EXTENSION}` placeholder matches the extension of the page, view, or component, either `razor` or `cshtml`, followed by `.js`.
22+
23+
In the following example from a Razor Pages app, the script is collocated in the `Pages` folder with the `Contact` page (`Pages/Contact.cshtml`):
24+
25+
```razor
26+
@section Scripts {
27+
<script src="~/Pages/Contact.cshtml.js"></script>
28+
}
29+
```
30+
31+
* For scripts provided by a Razor class library (RCL):
32+
33+
`_content/{PACKAGE ID}/{PATH}/{PAGE, VIEW, OR COMPONENT}.{EXTENSION}`
34+
35+
* The `{PACKAGE ID}` placeholder is the RCL's package identifier (or library name for a class library referenced by the app).
36+
* The `{PATH}` placeholder is the path to the page, view, or component. If a Razor component is located at the root of the RCL, the path segment isn't included.
37+
* The `{PAGE, VIEW, OR COMPONENT}` placeholder is the page, view, or component.
38+
* The `{EXTENSION}` placeholder matches the extension of page, view, or component, either `razor` or `cshtml`, followed by `.js`.
39+
40+
In the following Blazor app example:
41+
42+
* The RCL's package identifier is `AppJS`.
43+
* A module's scripts are loaded for the `Index` component (`Index.razor`).
44+
* The `Index` component is in the `Pages` folder of the RCL.
45+
46+
```csharp
47+
var module = await JS.InvokeAsync<IJSObjectReference>("import",
48+
"_content/AppJS/Pages/Index.razor.js");
49+
```

0 commit comments

Comments
 (0)