Skip to content

Refactor Plan

github-actions[bot] edited this page Dec 2, 2025 · 1 revision

Refactoring Plan - Best Practices Structure

Current Issues

1. Monolithic index.html (1,281 lines, 72KB)

  • ❌ 106 lines of CSS inline
  • ❌ 829 lines of JavaScript inline
  • ❌ Violates separation of concerns
  • ❌ Hard to maintain and debug
  • ❌ Poor caching strategy (entire file invalidated on any change)

2. Flat Directory Structure

flight_budget/
β”œβ”€β”€ index.html              ← App file
β”œβ”€β”€ libs/                   ← App dependencies
β”œβ”€β”€ data/                   ← App data
β”œβ”€β”€ nginx/                  ← Infrastructure
β”œβ”€β”€ Dockerfile              ← Infrastructure
β”œβ”€β”€ docker-compose.yml      ← Infrastructure
β”œβ”€β”€ README.md               ← Documentation
β”œβ”€β”€ DEPLOYMENT.md           ← Documentation
β”œβ”€β”€ QUICK_START.md          ← Documentation
β”œβ”€β”€ TODO.md                 ← Documentation
β”œβ”€β”€ CONTAINER_SETUP.md      ← Documentation
└── PROJECT_STRUCTURE.md    ← Documentation

Problems:

  • App files mixed with infrastructure
  • Documentation cluttering root
  • No clear boundaries
  • Hard to navigate

Recommended Structure (Best Practices)

flight_budget/
β”œβ”€β”€ πŸ“ app/                          ← Application files (deployed)
β”‚   β”œβ”€β”€ index.html                   ← HTML only (~350 lines)
β”‚   β”œβ”€β”€ css/
β”‚   β”‚   └── styles.css               ← Extracted CSS (~110 lines)
β”‚   β”œβ”€β”€ js/
β”‚   β”‚   └── app.js                   ← Extracted JS (~830 lines)
β”‚   β”œβ”€β”€ libs/                        ← JavaScript dependencies
β”‚   β”‚   β”œβ”€β”€ papaparse.min.js
β”‚   β”‚   β”œβ”€β”€ chart.umd.min.js
β”‚   β”‚   └── html2pdf.bundle.min.js
β”‚   └── data/                        ← Persistent data (volume mount)
β”‚       └── .gitkeep
β”‚
β”œβ”€β”€ πŸ“ docs/                         ← Documentation (NOT deployed)
β”‚   β”œβ”€β”€ README.md
β”‚   β”œβ”€β”€ DEPLOYMENT.md
β”‚   β”œβ”€β”€ QUICK_START.md
β”‚   β”œβ”€β”€ CONTAINER_SETUP.md
β”‚   β”œβ”€β”€ PROJECT_STRUCTURE.md
β”‚   β”œβ”€β”€ REFACTOR_PLAN.md
β”‚   └── TODO.md
β”‚
β”œβ”€β”€ πŸ“ infrastructure/               ← Docker/deployment configs
β”‚   β”œβ”€β”€ nginx/
β”‚   β”‚   └── nginx.conf
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── docker-compose.yml
β”‚
β”œβ”€β”€ πŸ“ .github/                      ← CI/CD
β”‚   └── workflows/
β”‚       β”œβ”€β”€ docker-build.yml
β”‚       └── update-dependencies.yml
β”‚
β”œβ”€β”€ .dockerignore
β”œβ”€β”€ .env.example
β”œβ”€β”€ .gitignore
└── README.md                        ← Short readme pointing to docs/

Benefits of Refactoring

1. Separation of Concerns

βœ… HTML for structure βœ… CSS for styling βœ… JavaScript for behavior βœ… Each file has single responsibility

2. Better Caching

βœ… CSS changes don't invalidate HTML cache βœ… JS changes don't invalidate CSS cache βœ… Browser caches files independently βœ… Faster load times after first visit

3. Improved Maintainability

βœ… Easier to find and edit code βœ… Syntax highlighting works better in separate files βœ… Easier for multiple developers to work simultaneously βœ… Easier to debug (browser dev tools work better)

4. Cleaner Structure

βœ… Clear separation: app vs infrastructure vs docs βœ… Docker builds only what's needed βœ… Documentation doesn't clutter root βœ… Easier to navigate project

5. Professional Organization

βœ… Industry standard structure βœ… Easier for new developers to understand βœ… Better for code reviews βœ… Scalable for future growth

Migration Steps

Phase 1: Extract CSS & JS (No Breaking Changes)

  1. Create app/css/styles.css - extract lines 10-116 from index.html
  2. Create app/js/app.js - extract lines 451-1280 from index.html
  3. Update app/index.html - link to external files
  4. Move index.html β†’ app/index.html
  5. Move libs/ β†’ app/libs/
  6. Move data/ β†’ app/data/

Phase 2: Organize Infrastructure

  1. Create infrastructure/ folder
  2. Move Dockerfile β†’ infrastructure/Dockerfile
  3. Move docker-compose.yml β†’ infrastructure/docker-compose.yml
  4. Move nginx/ β†’ infrastructure/nginx/
  5. Update paths in Dockerfile and compose file

Phase 3: Organize Documentation

  1. Create docs/ folder
  2. Move all .md files (except root README) β†’ docs/
  3. Create short root README.md pointing to docs
  4. Update internal links in documentation

Phase 4: Update Docker Configuration

  1. Update Dockerfile to copy from app/ directory
  2. Update nginx.conf if needed (should work as-is)
  3. Update .dockerignore to exclude docs and infrastructure
  4. Update GitHub Actions paths

Phase 5: Test & Validate

  1. Build Docker image locally
  2. Test all functionality
  3. Verify caching headers work
  4. Check file paths in browser console
  5. Test CSV upload/download
  6. Test PDF export

File Size Comparison

Current (Monolithic)

index.html:     72KB (1,281 lines)
Total loaded:   72KB on every page load

After Refactor

index.html:     ~15KB (350 lines)
styles.css:     ~5KB (110 lines)
app.js:         ~25KB (830 lines)
───────────────────────────────
Total:          ~45KB (40% smaller!)

After gzip:
index.html:     ~5KB
styles.css:     ~2KB
app.js:         ~8KB
───────────────────────────────
Total:          ~15KB (80% smaller!)

Caching Benefits:

  • First visit: 45KB download
  • Return visits: ~15KB (only HTML if JS/CSS cached)
  • After code change: Only changed file re-downloaded

Performance Impact

Current

Browser loads:
1. index.html (72KB) - contains everything
Total: 72KB every time

After Refactor

First visit:
1. index.html (15KB)
2. styles.css (5KB) - cached for 1 year
3. app.js (25KB) - cached for 1 year
Total: 45KB first time

Return visits:
1. index.html (15KB) - no cache (always fresh)
2. styles.css - from cache (0KB)
3. app.js - from cache (0KB)
Total: 15KB (70% faster!)

Docker Image Size Impact

Current

Base: 23MB
App: 72KB (index.html) + 1.1MB (libs)
Total: ~25MB

After Refactor

Base: 23MB
App: 45KB (html+css+js) + 1.1MB (libs)
Total: ~24MB (slightly smaller)

Note: Image size improvement is minimal, but organization is much better.

Nginx Caching Strategy

Current nginx.conf

location / {
    # No cache for HTML
    add_header Cache-Control "no-cache, no-store, must-revalidate";
}

After Refactor

# HTML - always fetch fresh
location ~ \.html$ {
    add_header Cache-Control "no-cache, no-store, must-revalidate";
}

# CSS/JS - cache for 1 year (immutable)
location ~ \.(css|js)$ {
    expires 1y;
    add_header Cache-Control "public, immutable";
}

# JS libraries - cache for 1 year
location ^~ /libs/ {
    expires 1y;
    add_header Cache-Control "public, immutable";
}

Implementation Complexity

Task Complexity Time Risk
Extract CSS Low 15 min None
Extract JS Low 15 min None
Reorganize folders Low 30 min Low
Update Dockerfile Medium 30 min Medium
Update nginx.conf Low 15 min Low
Update docs Low 30 min None
Test everything Medium 1 hour -
Total Low-Medium ~3 hours Low

Breaking Changes

None if done correctly!

The refactoring should be transparent to users:

  • Same functionality
  • Same URLs
  • Same behavior
  • Better performance
  • Better maintainability

What stays the same:

  • βœ… All features work identically
  • βœ… CSV import/export
  • βœ… PDF generation
  • βœ… Save/load budgets
  • βœ… Docker deployment process
  • βœ… Portainer compatibility

What improves:

  • βœ… Faster load times (caching)
  • βœ… Easier to maintain
  • βœ… Better debugging
  • βœ… Professional structure
  • βœ… Ready for future features

Recommendation

βœ… YES - Refactor Now

Reasons:

  1. Early in project lifecycle - easier now than later
  2. No users yet - no breaking changes to worry about
  3. Industry best practice - should have been done initially
  4. Better for future - easier to add features
  5. Low risk - straightforward changes
  6. Quick - only ~3 hours of work
  7. Professional - looks better to contributors/employers

Alternative: Keep as-is

Only if:

  • ❌ Project is throw-away/temporary
  • ❌ Only you will ever work on it
  • ❌ No plans to add features
  • ❌ Performance doesn't matter

But this doesn't apply because:

  • βœ… You're deploying to production (Portainer)
  • βœ… You're setting up CI/CD (GitHub Actions)
  • βœ… You created comprehensive documentation
  • βœ… You're asking about best practices

Proposed Action Plan

  1. Review this plan - make sure you agree with approach
  2. Backup current state - commit to git first
  3. Implement refactoring - follow phase-by-phase plan
  4. Test locally - ensure everything works
  5. Update documentation - reflect new structure
  6. Deploy - push to GitHub, auto-deploy via CI/CD

Questions to Answer

Before proceeding, decide:

  1. Do you want to refactor now or after initial deployment?

    • Recommendation: Now (easier and cleaner)
  2. Keep flat structure or use app/ folder?

    • Recommendation: Use app/ folder (professional)
  3. Move docs to docs/ or keep in root?

    • Recommendation: Move to docs/ (cleaner root)
  4. Extract CSS/JS or keep inline?

    • Recommendation: Extract (best practice)

Decision needed: Should we proceed with the refactor?

If YES, I can implement all changes in ~30-60 minutes with zero breaking changes.

If NO, we can keep current structure and proceed to deployment.

My recommendation: Refactor now. It's the right time and right approach.

Clone this wiki locally