James Williams
Birmingham Newman University
jwilliams@staff.newman.ac.uk
3-hour session
// React component example
function Welcome(props) {
return <h1>Hello, {props.name}!</h1>;
}
// Vue component example
<template>
<h1>Hello {{ name }}!</h1>
</template>
// Service Worker registration
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('/sw.js')
.then(registration => {
console.log('SW registered');
});
}
// Web App Manifest
{
"name": "My PWA",
"short_name": "PWA",
"start_url": "/",
"display": "standalone"
}
// Custom element definition
class MyElement extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'});
}
connectedCallback() {
this.shadowRoot.innerHTML = `
<style>h1 { color: blue; }</style>
<h1>Hello from Web Component!</h1>
`;
}
}
customElements.define('my-element', MyElement);
// TypeScript example
interface User {
id: number;
name: string;
email: string;
}
function createUser(userData: User): User {
return {
id: userData.id,
name: userData.name,
email: userData.email
};
}
const user = createUser({
id: 1,
name: "John",
email: "john@example.com"
});
// Styled Components example
const Button = styled.button`
background: ${props => props.primary ? 'blue' : 'white'};
color: ${props => props.primary ? 'white' : 'black'};
padding: 10px 20px;
border: 2px solid blue;
border-radius: 5px;
`;
// Usage
<Button primary>Primary Button</Button>
<Button>Secondary Button</Button>
// Redux example
const initialState = {
count: 0
};
function counterReducer(state = initialState, action) {
switch (action.type) {
case 'INCREMENT':
return { count: state.count + 1 };
case 'DECREMENT':
return { count: state.count - 1 };
default:
return state;
}
}
// Webpack configuration
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
module: {
rules: [
{ test: /\.js$/, use: 'babel-loader' },
{ test: /\.css$/, use: ['style-loader', 'css-loader'] }
]
}
};
// WebAssembly example
WebAssembly.instantiateStreaming(
fetch('module.wasm')
).then(obj => {
const result = obj.instance.exports.add(1, 2);
console.log(result); // 3
});
Time: 45 minutes
This task will help you understand modern frameworks
Take a break, ask questions, or catch up on the previous task.
Next: Secondary slides and Task 2
// Code splitting
const LazyComponent = React.lazy(() => import('./LazyComponent'));
// Service worker caching
self.addEventListener('fetch', event => {
event.respondWith(
caches.match(event.request)
.then(response => response || fetch(event.request))
);
});
// CSP header
Content-Security-Policy: default-src 'self'; script-src 'self'
// SRI example
<script src="https://example.com/script.js"
integrity="sha384-..."
crossorigin="anonymous"></script>
// Jest test example
test('renders user name', () => {
render(<UserProfile name="John" />);
expect(screen.getByText('John')).toBeInTheDocument();
});
// Cypress test
it('should login successfully', () => {
cy.visit('/login');
cy.get('[data-testid=email]').type('user@example.com');
cy.get('[data-testid=password]').type('password');
cy.get('[data-testid=submit]').click();
cy.url().should('include', '/dashboard');
});
// GitHub Actions workflow
name: Deploy to Production
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Deploy
run: npm run deploy
// ARIA live region
<div aria-live="polite" aria-atomic="true">
Status updates appear here
</div>
// Focus management
function trapFocus(element) {
const focusableElements = element.querySelectorAll(
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
);
}
// i18n example
const messages = {
en: {
welcome: 'Welcome'
},
es: {
welcome: 'Bienvenido'
}
};
// Date formatting
const date = new Date().toLocaleDateString('es-ES');
// Module Federation
new ModuleFederationPlugin({
name: 'host',
remotes: {
remote: 'remote@http://localhost:3001/remoteEntry.js'
}
});
// Web3.js example
const web3 = new Web3(Web3.givenProvider);
// Connect to wallet
async function connectWallet() {
const accounts = await ethereum.request({
method: 'eth_requestAccounts'
});
}
// TensorFlow.js example
const model = tf.sequential({
layers: [
tf.layers.dense({units: 1, inputShape: [1]})
]
});
model.compile({
loss: 'meanSquaredError',
optimizer: 'sgd'
});
// Speech recognition
const recognition = new webkitSpeechRecognition();
recognition.continuous = true;
recognition.onresult = function(event) {
const transcript = event.results[0][0].transcript;
console.log('Heard:', transcript);
};
// Speech synthesis
const utterance = new SpeechSynthesisUtterance('Hello World');
speechSynthesis.speak(utterance);
// WebXR example
navigator.xr.isSessionSupported('immersive-vr')
.then(supported => {
if (supported) {
console.log('VR supported');
}
});
// Three.js scene
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
Time: 45 minutes
This task will help you explore cutting-edge web technologies
Final Project and Assessment - Bringing it all together