Naja AJAX Library for Nette Framework
Naja is a modern, full-featured client-side AJAX library specifically designed for seamless integration with the Nette Framework. It provides robust capabilities for handling asynchronous requests, managing browser history states, and dynamically updating UI snippets, enabling developers to build highly interactive web applications with minimal JavaScript boilerplate. Currently stable at version 3.3.3, Naja maintains an active release cadence, frequently delivering minor enhancements and bug fixes. Key differentiators include its tight coupling with Nette's server-side AJAX responses, thorough testing across modern browsers (Chromium, Firefox, WebKit), and a strong focus on providing a reliable, performant AJAX experience tailored for the Nette ecosystem, allowing for progressive enhancement of traditional server-rendered applications. It is written using modern JavaScript and compiled for broad browser compatibility.
Common errors
-
Uncaught TypeError: naja.initialize is not a function
cause Attempting to call `initialize()` on an incorrectly imported or undefined Naja object.fixEnsure `import naja from 'naja';` is at the top of your script and that the script is executed after Naja is loaded and parsed as an ES Module. -
Uncaught ReferenceError: naja is not defined
cause Naja's global object is not available when the script attempts to access it, usually due to missing or incorrect module import.fixVerify that Naja is correctly imported as a module (`import naja from 'naja';`) and that your build/runtime environment supports ES Modules. Avoid relying on a globally exposed `naja` without proper import. -
Cannot read properties of undefined (reading 'registerFactory') for custom extensions
cause Attempting to access or extend Naja's internal components (like handlers or factories) in a way that is incompatible with Naja 3.x's refactored internals.fixReview the Naja 3.x documentation for updated APIs and extension points. The internals for snippet caching and handler registration might have changed, requiring updates to custom extensions.
Warnings
- breaking Naja 3.0 introduced significant internal changes to the snippet cache, which might alter behavior for advanced users or those with custom extensions. Thorough testing of existing applications with Naja 3.x is strongly recommended.
- breaking As of Naja 3.2.0, UI binding to forms has been simplified. Naja no longer binds directly to individual submitter elements (like buttons) but instead listens only to the `submit` event on the form itself. This can affect custom event handlers or extensions that previously relied on direct binding to submitter elements.
- gotcha Naja is primarily designed as an ES Module (ESM). Using CommonJS `require()` to import Naja can lead to `TypeError: naja.initialize is not a function` or similar module resolution errors.
Install
-
npm install naja -
yarn add naja -
pnpm add naja
Imports
- naja
const naja = require('naja');import naja from 'naja';
- Event types
import { InteractionEvent, BuildStateEvent } from 'naja';import type { InteractionEvent, BuildStateEvent } from 'naja'; - Naja instance (global)
naja.initialize(); // without import
import naja from 'naja'; naja.initialize();
Quickstart
import naja from 'naja';
document.addEventListener('DOMContentLoaded', () => {
// Initialize Naja. This sets up event listeners for AJAX interactions
// on elements like <a data-nette-ajax> and <form data-nette-ajax>.
naja.initialize();
// Listen to Naja's lifecycle events for debugging or custom logic.
naja.addEventListener('init', () => {
console.log('Naja initialized and ready to handle Nette AJAX requests.');
});
naja.addEventListener('interaction', (event) => {
console.log('AJAX interaction started:', event.detail.element.tagName, event.detail.url);
});
naja.addEventListener('start', (event) => {
console.log('AJAX request is about to be sent.');
// You could show a loading spinner here
});
naja.addEventListener('success', (event) => {
console.log('AJAX request successful.');
// Hide loading spinner
});
naja.addEventListener('error', (event) => {
console.error('AJAX request failed:', event.detail.error);
// Handle error, e.g., display an error message
});
// Example: Custom snippet handler for specific element updates
naja.snippetHandler.addEventListener('afterUpdate', (event) => {
const { snippet, content } = event.detail;
if (snippet && snippet.id === 'my-dynamic-content') {
document.getElementById('my-dynamic-content').innerHTML = content;
console.log(`Snippet #${snippet.id} updated.`);
}
});
});