Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions common/src/gallery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export const GalleryDataSchema = z.object({
metadata: GalleryMetadataSchema,
mediaBaseUrl: z.string().optional(),
thumbsBaseUrl: z.string().optional(),
assetsBaseUrl: z.string().optional(),
analyticsScript: z.string().optional(),
ctaBanner: z.boolean().optional(),
sections: z.array(GallerySectionSchema),
Expand Down
40 changes: 40 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,46 @@ Markdown formatting is available in:

Thumbnails will automatically be generated using sizes that fit the theme (300px height and 600px height for retina displays). If you want, you can change the size using the `thumbnailSize` attribute in the `gallery.json` file.

## Static assets (CSS and JavaScript)

By default, each gallery includes its own copy of the static asset files (CSS and JavaScript) in the `gallery/assets/` folder. However, if you're hosting multiple galleries built with the same version of Simple Photo Gallery, you can share these assets between all galleries to reduce storage and improve caching.

Use the `assetsBaseUrl` option to specify the base URL where the shared static assets are hosted:

```json
{
"title": "My Gallery",
"description": "...",
"assetsBaseUrl": "https://cdn.example.com/simple-photo-gallery/v1.0.0"
}
```

When set, the generated HTML will load CSS and JavaScript files from the specified URL instead of the local `gallery/assets/` folder:

```html
<link rel="stylesheet" href="https://cdn.example.com/simple-photo-gallery/v1.0.0/assets/style.xxx.css">
<script type="module" src="https://cdn.example.com/simple-photo-gallery/v1.0.0/assets/PhotoSwipe.xxx.js">
```

### Shared assets setup

To use shared assets across multiple galleries:

1. **Build one gallery** to generate the asset files in `gallery/assets/`
2. **Upload the assets folder** to your CDN or static hosting (e.g., `https://cdn.example.com/simple-photo-gallery/v1.0.0/assets/`)
3. **Set `assetsBaseUrl`** in each gallery's `gallery.json` to point to the shared location
4. **Rebuild the galleries** to update the HTML references

> **Tip:** Include the version number in your assets URL path. This ensures that when you upgrade Simple Photo Gallery and rebuild your galleries, the new assets won't conflict with cached versions from older builds.

### Benefits

- **Reduced storage**: Asset files are stored once instead of duplicated in every gallery
- **Better caching**: Browsers cache the shared assets, so visitors loading multiple galleries only download them once
- **Faster deployments**: Only upload gallery-specific files (images, thumbnails, `gallery.json`) when updating content

If `assetsBaseUrl` is not specified, the default behavior is to use local assets from the `gallery/` folder.

## Analytics script

You can add custom analytics scripts (such as Google Analytics, Plausible, or other tracking services) to your gallery by including an `analyticsScript` field in your `gallery.json` file. The script will be embedded at the end of the HTML body tag.
Expand Down
8 changes: 8 additions & 0 deletions gallery/src/modules/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,14 @@ async function buildGallery(
process.env.GALLERY_JSON_PATH = galleryJsonPath;
process.env.GALLERY_OUTPUT_DIR = path.join(galleryDir, 'gallery');

// Set the assets base URL if specified in the gallery data
if (galleryData.assetsBaseUrl) {
process.env.GALLERY_ASSETS_BASE_URL = galleryData.assetsBaseUrl;
} else {
// Clear the environment variable to use the default value
delete process.env.GALLERY_ASSETS_BASE_URL;
}

execSync('npx astro build', { cwd: templateDir, stdio: ui.level === LogLevels.debug ? 'inherit' : 'ignore' });
} catch (error) {
ui.error(`Build failed for ${galleryDir}`);
Expand Down
5 changes: 4 additions & 1 deletion themes/modern/astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ if (!sourceGalleryPath) throw new Error('GALLERY_JSON_PATH environment variable

const outputDir = process.env.GALLERY_OUTPUT_DIR || sourceGalleryPath.replace('gallery.json', '');

// Get the assets base URL from environment variable (defaults to 'gallery' for local assets)
const assetsBaseUrl = process.env.GALLERY_ASSETS_BASE_URL || 'gallery';

/**
* Astro integration to prevent empty content collection files from being generated
* These files (content-assets.mjs and content-modules.mjs) are generated by Astro
Expand Down Expand Up @@ -49,7 +52,7 @@ export default defineConfig({
outDir: outputDir + '/_build',
build: {
assets: 'assets',
assetsPrefix: 'gallery',
assetsPrefix: assetsBaseUrl,
},
integrations: [relativeLinks(), preventEmptyContentFiles()],
publicDir: 'public',
Expand Down