nextjs 1.7.0
Install from the command line:
Learn more about npm packages
$ npm install @snapkit-studio/nextjs@1.7.0
Install via package.json:
"@snapkit-studio/nextjs": "1.7.0"
About this version
Next.js image loader and React component for Snapkit image optimization service.
- πΌοΈ Next.js Image Integration: Seamless integration with Next.js Image component
- β‘ Automatic Optimization: Dynamic image transformation with DPR-based srcset
- π― Flexible Configuration: Global and per-component configuration options
- π± Format Auto-Selection: Intelligent format selection (WebP, AVIF, etc.)
- π Server Components Support: Automatic server/client component selection
- π§ TypeScript Support: Full TypeScript definitions included
- π§ͺ Well Tested: 90%+ test coverage with comprehensive edge case handling
- π‘οΈ Memory Leak Prevention: Advanced cleanup logic for IntersectionObserver and resources
- π¦ Picture Element Support: Server-side rendering with optimized srcSet generation
npm install @snapkit-studio/nextjs
# or
yarn add @snapkit-studio/nextjs
# or
pnpm add @snapkit-studio/nextjs# .env.local (Required and Optional variables)
NEXT_PUBLIC_SNAPKIT_ORGANIZATION_NAME=your-organization-name # Required
NEXT_PUBLIC_SNAPKIT_DEFAULT_QUALITY=85 # Optional (default: 85)
NEXT_PUBLIC_SNAPKIT_DEFAULT_OPTIMIZE_FORMAT=auto # Optional (default: auto)// app/components/MyComponent.tsx
import { Image } from '@snapkit-studio/nextjs';
export function Hero() {
return (
<Image
src="/hero-image.jpg"
width={1200}
height={600}
alt="Hero image"
transforms={{
format: 'auto',
quality: 85
}}
/>
);
}Best for most applications where you want consistent optimization across all images.
# .env.local - All available environment variables
NEXT_PUBLIC_SNAPKIT_ORGANIZATION_NAME=your-organization-name # Required
NEXT_PUBLIC_SNAPKIT_DEFAULT_QUALITY=85 # Optional (1-100, default: 85)
NEXT_PUBLIC_SNAPKIT_DEFAULT_OPTIMIZE_FORMAT=auto # Optional (auto|avif|webp|off, default: auto)Environment Variables Reference:
-
NEXT_PUBLIC_SNAPKIT_ORGANIZATION_NAME: Your Snapkit organization identifier (required) -
NEXT_PUBLIC_SNAPKIT_DEFAULT_QUALITY: Global quality setting for all images (1-100) -
NEXT_PUBLIC_SNAPKIT_DEFAULT_OPTIMIZE_FORMAT: Default format optimization strategy-
auto: Automatically select best format based on browser support -
avif: Use AVIF format if supported -
webp: Use WebP format if supported -
off: Disable format optimization
-
// Use anywhere in your app - no setup required
import { Image } from '@snapkit-studio/nextjs';
<Image src="/photo.jpg" width={800} height={600} alt="Photo" />Use when you need different optimization settings for specific image components.
import { Image } from '@snapkit-studio/nextjs';
<Image
src="/high-quality-photo.jpg"
width={1920}
height={1080}
alt="High quality photo"
organizationName="custom-org"
transforms={{
format: 'avif',
quality: 90
}}
/>For maximum control or integration with custom image components.
import { snapkitLoader } from '@snapkit-studio/nextjs';
// Get optimized URL directly
const optimizedUrl = snapkitLoader({
src: '/my-image.jpg',
width: 800,
quality: 85,
});
console.log(optimizedUrl);
// Output: "https://your-org-name.snapkit.studio/transform/w_800,q_85,f_auto/my-image.jpg"interface ImageTransforms {
/** Output format: 'auto', 'webp', 'avif', 'jpeg', 'png' */
format?: string;
/** Image quality: 1-100 */
quality?: number;
/** Image width in pixels */
width?: number;
/** Image height in pixels */
height?: number;
/** Blur radius: 0-100 */
blur?: number;
/** Additional transforms... */
[key: string]: any;
}The Next.js package now includes automatic server/client component selection for optimal performance:
import { Image } from '@snapkit-studio/nextjs';
// Automatically renders as a server component (no client JS)
<Image
src="/hero.jpg"
width={1200}
height={600}
alt="Hero image"
/>
// Automatically switches to client component (has onLoad handler)
<Image
src="/interactive.jpg"
width={800}
height={400}
alt="Interactive image"
onLoad={() => console.log('Loaded!')}
/>| Props | Renders As | Reason |
|---|---|---|
| Basic props only | ServerImage |
Default, best performance with server-side srcSet |
onLoad, onError
|
ClientImage |
Event handlers need browser |
adjustQualityByNetwork |
ClientImage |
Network detection needs browser |
dprOptions |
ClientImage |
Custom DPR detection needs browser |
optimize="server" |
ServerImage |
Explicitly forced server rendering |
optimize="client" |
ClientImage |
Explicitly forced client rendering |
// Force server rendering
<Image
src="/static.jpg"
optimize="server"
alt="Always server rendered"
/>
// Force client rendering
<Image
src="/dynamic.jpg"
optimize="client"
alt="Always client rendered"
/>For cases where you need direct access to specific components:
import { ServerImage, ClientImage } from '@snapkit-studio/nextjs';
// Use ServerImage directly (no 'use client' needed)
export default function ServerComponent() {
return <ServerImage src="/image.jpg" width={800} height={600} alt="Server" />;
}
// Use ClientImage in client components
'use client';
export default function ClientComponent() {
return <ClientImage src="/image.jpg" width={800} height={600} alt="Client" />;
}// Override environment variable per component
import { Image } from '@snapkit-studio/nextjs';
export function Gallery({ images }: { images: string[] }) {
return (
<div className="grid grid-cols-3 gap-4">
{images.map((src, index) => (
<Image
key={index}
src={src}
width={400}
height={300}
alt={`Gallery image ${index + 1}`}
organizationName="custom-org"
transforms={{ format: 'auto', quality: 80 }}
/>
))}
</div>
);
}<Image
src="/portrait.jpg"
width={600}
height={800}
alt="Portrait"
transforms={{
format: 'webp',
quality: 85,
blur: 2, // Slight blur effect
}}
/>/** @type {import('next').NextConfig} */
const nextConfig = {
images: {
// Allow Snapkit domains
domains: ['your-org-name.snapkit.studio'],
// Use custom loader globally (optional)
loader: 'custom',
loaderFile: './snapkit-loader.js',
},
};
module.exports = nextConfig;// snapkit-loader.js
import { snapkitLoader } from '@snapkit-studio/nextjs';
export default snapkitLoader;Experience all features in action with our interactive demo:
π Live Demo β - Real-time examples with source code
Explore features including:
- Server/Client component automatic selection
- DPR optimization with srcSet generation
- Image transformations with live preview
- Picture element server-side rendering
- All optimization features and configurations
// Before: Next.js built-in
<Image
src="/image.jpg"
width={800}
height={600}
alt="Image"
/>
// After: Snapkit optimization
import { Image } from '@snapkit-studio/nextjs';
<Image
src="/image.jpg"
width={800}
height={600}
alt="Image"
transforms={{ format: 'webp', quality: 85 }}
/>// Before: Cloudinary
<Image
src="https://res.cloudinary.com/demo/image/upload/w_800,q_85/sample.jpg"
width={800}
height={600}
/>
// After: Snapkit
<Image
src="/sample.jpg"
width={800}
height={600}
transforms={{ width: 800, quality: 85 }}
/>Default image loader using global configuration.
Parameters:
-
src: Image source path -
width: Target width in pixels -
quality: Image quality (1-100)
Returns: Optimized image URL
Create a custom image loader with specific configuration.
Returns: Image loader function
React component wrapper around Next.js Image with Snapkit optimization.
Props: All Next.js Image props plus:
-
transforms?: ImageTransforms- Image transformations to apply
The package includes comprehensive test coverage with automatic coverage reporting:
# Run tests
npm test
# Run with coverage report
npm run test:coverage
# Watch mode for development
npm test -- --watchThe package maintains high test coverage standards:
- Coverage Threshold: 80% minimum for branches, functions, lines, and statements
- Test Framework: Vitest with jsdom environment and v8 coverage provider
- Coverage Reports: Text (console), JSON, HTML, and LCOV formats
- Coverage Exclusions: Test setup files, configuration files, type definitions, and test utilities
Coverage reports are generated in multiple formats:
- Text: Console output during test runs
-
HTML: Detailed coverage report in
coverage/directory - LCOV: For CI/CD integration and coverage tools
- JSON: Machine-readable coverage data
Tests run in a jsdom environment to simulate browser behavior for React Server Components and Client Components testing.
Contributions are welcome! Please read our contributing guide for details on our code of conduct and development process.
MIT Β© Snapkit
Details
- nextjs
-
snapkit-studio
- 6 months ago
- MIT
- 14 dependencies
Assets
- nextjs-1.7.0.tgz
Download activity
- Total downloads 0
- Last 30 days 0
- Last week 0
- Today 0