\n \n\n\n```\n\n\n
\n\n\n## Security Considerations\n * **Access Control** - Ensure that sensitive files are not placed in the\n `public` directory as they will be publicly accessible.\n\n\n
\n\n\nBy organizing your static assets efficiently and using SherpaJS's built-in\ncapabilities, you can serve these resources effectively to enhance the\nperformance and usability of your web applications.","headings":[{"id":"defining-static-assets-directory","title":"Defining Static Assets Directory","rank":2},{"id":"serving-static-assets","title":"Serving Static Assets","rank":2},{"id":"examples","title":"Examples","rank":2},{"id":"view-endpoints-example","title":"View Endpoints Example","rank":3},{"id":"security-considerations","title":"Security Considerations","rank":2}],"frontmatter":{},"code":"/*@jsxRuntime automatic @jsxImportSource react*/\nconst {Fragment: _Fragment, jsx: _jsx, jsxs: _jsxs} = arguments[0];\nfunction _createMdxContent(props) {\n const _components = Object.assign({\n section: \"section\",\n h1: \"h1\",\n p: \"p\",\n h2: \"h2\",\n code: \"code\",\n pre: \"pre\",\n ul: \"ul\",\n li: \"li\",\n h3: \"h3\",\n strong: \"strong\"\n }, props.components);\n return _jsxs(_Fragment, {\n children: [_jsxs(_components.section, {\n id: \"static-assets\",\n children: [_jsx(_components.h1, {\n id: \"static-assets\",\n children: \"Static Assets\"\n }), \"\\n\", _jsx(_components.p, {\n children: \"SherpaJS servers can serve static assets such as images, CSS, JavaScript\\nfiles, and other resources that do not change frequently. These assets are\\nplaced in a dedicated folder within your project and are served directly to\\nclients when requested.\"\n }), \"\\n\", _jsx(_components.p, {\n children: \"This static asset directory is optional. Static assets are not transferred from\\nmodules.\"\n }), \"\\n\", _jsx(\"br\", {}), \"\\n\"]\n }), _jsxs(_components.section, {\n id: \"defining-static-assets-directory\",\n children: [_jsx(_components.h2, {\n id: \"defining-static-assets-directory\",\n children: \"Defining Static Assets Directory\"\n }), \"\\n\", _jsxs(_components.p, {\n children: [\"The default directory for static assets in a SherpaJS project is the \", _jsx(_components.code, {\n children: \"public\"\n }), \"\\nfolder. Any files placed in this folder are accessible via a URL path that\\nmirrors the directory structure within \", _jsx(_components.code, {\n children: \"public\"\n }), \".\"]\n }), \"\\n\", _jsx(\"br\", {}), \"\\n\"]\n }), _jsxs(_components.section, {\n id: \"serving-static-assets\",\n children: [_jsx(_components.h2, {\n id: \"serving-static-assets\",\n children: \"Serving Static Assets\"\n }), \"\\n\", _jsxs(_components.p, {\n children: [\"To serve static assets, simply place the files within the \", _jsx(_components.code, {\n children: \"public\"\n }), \"\\ndirectory of your project. For example:\"]\n }), \"\\n\", _jsx(_components.pre, {\n raw: \"/your-project\\n│\\n├── /public\\n│ ├── /images\\n│ │ └── logo.png\\n│ ├── /css\\n│ │ └── styles.css\\n│ └── /js\\n│ └── script.js\\n│\\n├── /routes\\n│ └── view.html\\n└── sherpa.server.ts\\n\",\n language: \"text\",\n html: \"
/your-project\\n\\n├── /public\\n│   ├── /images\\n│   │   └── logo.png\\n│   ├── /css\\n│   │   └── styles.css\\n│   └── /js\\n│       └── script.js\\n\\n├── /routes\\n│   └── view.html\\n└── sherpa.server.ts\\n
\",\n children: _jsx(_components.code, {\n className: \"language-plaintext\",\n children: \"/your-project\\n│\\n├── /public\\n│ ├── /images\\n│ │ └── logo.png\\n│ ├── /css\\n│ │ └── styles.css\\n│ └── /js\\n│ └── script.js\\n│\\n├── /routes\\n│ └── view.html\\n└── sherpa.server.ts\\n\"\n })\n }), \"\\n\", _jsx(_components.p, {\n children: \"In this structure:\"\n }), \"\\n\", _jsxs(_components.ul, {\n children: [\"\\n\", _jsxs(_components.li, {\n children: [_jsx(_components.code, {\n children: \"logo.png\"\n }), \" will be accessible at \", _jsx(_components.code, {\n children: \"/images/logo.png\"\n })]\n }), \"\\n\", _jsxs(_components.li, {\n children: [_jsx(_components.code, {\n children: \"styles.css\"\n }), \" will be accessible at \", _jsx(_components.code, {\n children: \"/css/styles.css\"\n })]\n }), \"\\n\", _jsxs(_components.li, {\n children: [_jsx(_components.code, {\n children: \"script.js\"\n }), \" will be accessible at \", _jsx(_components.code, {\n children: \"/js/script.js\"\n })]\n }), \"\\n\", _jsxs(_components.li, {\n children: [_jsx(_components.code, {\n children: \"view.html\"\n }), \" will be accessible at \", _jsx(_components.code, {\n children: \"/\"\n })]\n }), \"\\n\"]\n }), \"\\n\", _jsx(\"br\", {}), \"\\n\"]\n }), _jsxs(_components.section, {\n id: \"examples\",\n children: [_jsx(_components.h2, {\n id: \"examples\",\n children: \"Examples\"\n }), \"\\n\"]\n }), _jsxs(_components.section, {\n id: \"view-endpoints-example\",\n children: [_jsx(_components.h3, {\n id: \"view-endpoints-example\",\n children: \"View Endpoints Example\"\n }), \"\\n\", _jsx(_components.p, {\n children: \"If you want to reference these static assets in your view endpoint or other\\nfiles, use the appropriate URL path. For example, in an HTML file:\"\n }), \"\\n\", _jsx(_components.pre, {\n raw: \"\\n\\n\\n \\n \\n My SherpaJS App\\n \\n\\n\\n
\\n \\\"Logo\\\"\\n
\\n
\\n

Welcome to My SherpaJS App

\\n

This is a simple example of serving static assets.

\\n \\n
\\n\\n\\n\",\n language: \"html\",\n html: \"
<!DOCTYPE html>\\n<html lang=\\\"en\\\">\\n<head>\\n    <meta charset=\\\"UTF-8\\\">\\n    <meta name=\\\"viewport\\\" content=\\\"width=device-width, initial-scale=1.0\\\">\\n    <title>My SherpaJS App</title>\\n    <link rel=\\\"stylesheet\\\" href=\\\"/css/styles.css\\\">\\n</head>\\n<body>\\n    <header>\\n        <img src=\\\"/images/logo.png\\\" alt=\\\"Logo\\\">\\n    </header>\\n    <main>\\n        <h1>Welcome to My SherpaJS App</h1>\\n        <p>This is a simple example of serving static assets.</p>\\n        <script src=\\\"/js/script.js\\\"></script>\\n    </main>\\n</body>\\n</html>\\n
\",\n title: \"/routes/view.html\",\n children: _jsx(_components.code, {\n className: \"language-html\",\n children: \"\\n\\n\\n \\n \\n My SherpaJS App\\n \\n\\n\\n
\\n \\\"Logo\\\"\\n
\\n
\\n

Welcome to My SherpaJS App

\\n

This is a simple example of serving static assets.

\\n \\n
\\n\\n\\n\"\n })\n }), \"\\n\", _jsx(\"br\", {}), \"\\n\"]\n }), _jsxs(_components.section, {\n id: \"security-considerations\",\n children: [_jsx(_components.h2, {\n id: \"security-considerations\",\n children: \"Security Considerations\"\n }), \"\\n\", _jsxs(_components.ul, {\n children: [\"\\n\", _jsxs(_components.li, {\n children: [_jsx(_components.strong, {\n children: \"Access Control\"\n }), \" - Ensure that sensitive files are not placed in the\\n\", _jsx(_components.code, {\n children: \"public\"\n }), \" directory as they will be publicly accessible.\"]\n }), \"\\n\"]\n }), \"\\n\", _jsx(\"br\", {}), \"\\n\", _jsx(_components.p, {\n children: \"By organizing your static assets efficiently and using SherpaJS's built-in\\ncapabilities, you can serve these resources effectively to enhance the\\nperformance and usability of your web applications.\"\n })]\n })]\n });\n}\nfunction MDXContent(props = {}) {\n const {wrapper: MDXLayout} = props.components || ({});\n return MDXLayout ? _jsx(MDXLayout, Object.assign({}, props, {\n children: _jsx(_createMdxContent, props)\n })) : _createMdxContent(props);\n}\nreturn {\n default: MDXContent\n};\n"},"preview":false}

Static Assets

SherpaJS servers can serve static assets such as images, CSS, JavaScript files, and other resources that do not change frequently. These assets are placed in a dedicated folder within your project and are served directly to clients when requested.

This static asset directory is optional. Static assets are not transferred from modules.


Defining Static Assets Directory

The default directory for static assets in a SherpaJS project is the public folder. Any files placed in this folder are accessible via a URL path that mirrors the directory structure within public.


Serving Static Assets

To serve static assets, simply place the files within the public directory of your project. For example:

/your-project

├── /public
│   ├── /images
│   │   └── logo.png
│   ├── /css
│   │   └── styles.css
│   └── /js
│       └── script.js

├── /routes
│   └── view.html
└── sherpa.server.ts

In this structure:

  • logo.png will be accessible at /images/logo.png
  • styles.css will be accessible at /css/styles.css
  • script.js will be accessible at /js/script.js
  • view.html will be accessible at /

Examples

View Endpoints Example

If you want to reference these static assets in your view endpoint or other files, use the appropriate URL path. For example, in an HTML file:

/routes/view.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My SherpaJS App</title>
    <link rel="stylesheet" href="/css/styles.css">
</head>
<body>
    <header>
        <img src="/images/logo.png" alt="Logo">
    </header>
    <main>
        <h1>Welcome to My SherpaJS App</h1>
        <p>This is a simple example of serving static assets.</p>
        <script src="/js/script.js"></script>
    </main>
</body>
</html>

Security Considerations

  • Access Control - Ensure that sensitive files are not placed in the public directory as they will be publicly accessible.

By organizing your static assets efficiently and using SherpaJS's built-in capabilities, you can serve these resources effectively to enhance the performance and usability of your web applications.